Find First and Last Employee Salaries

Find the first and last employee salary in each department using window functions.

sql

SELECT DISTINCT department_id, FIRST_VALUE(salary) OVER (PARTITION BY department_id ORDER BY salary ASC) AS first_salary, LAST_VALUE(salary) OVER (PARTITION BY department_id ORDER BY salary ASC RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) AS last_salary FROM employees;

This query uses the FIRST_VALUE and LAST_VALUE window functions partitioned by department_id to identify the minimum and maximum salary values within each department.

PromptDB can make mistakes. Please double-check responses.