SQL interview question: Top 3 Salaries per Department

Identify Top 3 Earners per Department

Management wants to recognize the top 3 highest-paid employees in each department for a performance bonus program.

Return dept_name, employee_name (full name), and salary for the top 3 earners in each department using DENSE_RANK().

Sort by department name, then salary descending.

Inspired by LeetCode's "Department Top Three Salaries"

Tables

employees 37 rows
  • id integer PK
  • first_name text
  • last_name text
  • email text
  • department_id integer → departments
  • salary integer
  • hire_date text
  • manager_id integer → employees
  • title text
idfirst_namelast_nameemaildepartment_idsalaryhire_datemanager_idtitle
1AliceChen[email protected]12500002015-01-15NULLCEO
2RobertMartinez[email protected]21800002016-03-20NULLCEO
3SarahJohnson[email protected]11500002017-06-101VP Engineering
departments 5 rows
  • id integer PK
  • name text
  • location text
idnamelocation
1EngineeringSan Francisco
2SalesNew York
3MarketingLos Angeles

Keep going