call(back)
SQL & DatamediumSign in to track progress

Top Earners per Department

Given tables employees(id, name, salary, department_id) and departments(id, name), write a query returning each department's name alongside its highest-paid employees, including ties.

Follow-up: return the top three per department, and compare the performance of a window-function solution against a correlated subquery.

Hints

Hint 1

DENSE_RANK() OVER (PARTITION BY department_id ORDER BY salary DESC) handles ties cleanly.

Hint 2

Window functions cannot appear in WHERE — filter on the rank in an outer query or CTE.

Asked at