Search code examples
sqlsqlitedatetimewindow-functions

SQL query to find employee with 3 year over year salary raises?


Here is the table. My initial observation would be to query the salary to which employee has an incremental salary per year, but am confused on how to do that. Employee 1 is the only employee that has a three year increase, but not sure how to single them out. Thanks!

enter image description here


Solution

  • One option uses aggregation:

    select employee_id
    from mytable t
    group by employee_id
    having max(salary) filter(where year = 2020) > max(salary) filter(where year = 2019)
       and max(salary) filter(where year = 2019) > max(salary) filter(where year = 2018)
    

    This brings employee whose 2020 salary is greather than their 2019 salary, and whose 2019 salary is greater than their 2018 salary - which is how I understood your question.