Find Top Salary Employees Per Department

Find employees with salary rank 1 in each department.

sql

SELECT id, name, department_id, salary FROM ( SELECT id, name, department_id, salary, RANK() OVER (PARTITION BY department_id ORDER BY salary DESC) as salary_rank FROM employees ) ranked_employees WHERE salary_rank = 1;

This query uses the RANK() window function to assign a rank to each employee within their respective department based on salary in descending order, then filters the result set to return only those with a rank of 1.

PromptDB can make mistakes. Please double-check responses.