Skip to main content
09

Top three salaries in each department

Hardwindow functionsDENSE_RANKtop-n-per-group

Your code isn't saved yet: refreshing or leaving the page resets it. Saved progress comes with user accounts.

A company wants to find the high earners in each department: employees whose salary is one of the top three distinct salaries of their department.

Return the columns department (the department name), employee and salary. Rows can be returned in any order.

Example

departments

idname
1Engineering
2Sales

employees

idnamesalarydepartment_id
1Noa850001
2Avi600001
3Dana850001
4Yossi900001
5Maya690001
6Eli700001
7Tal800002
8Roni600002

Result:

departmentemployeesalary
EngineeringYossi90000
EngineeringNoa85000
EngineeringDana85000
EngineeringEli70000
SalesTal80000
SalesRoni60000

Engineering's top three distinct salaries are 90000, 85000 and 70000. Noa and Dana share 85000, so four people make the list. Sales has only two employees, and both qualify.

Tip

Which ranking function gives tied salaries the same rank without leaving a gap after them?

Schema

sql
CREATE TABLE departments (
  id   INT PRIMARY KEY,
  name TEXT NOT NULL
);

CREATE TABLE employees (
  id            INT PRIMARY KEY,
  name          TEXT NOT NULL,
  salary        INT NOT NULL,
  department_id INT NOT NULL REFERENCES departments (id)
);

Approach

Rank salaries inside each department with DENSE_RANK and keep ranks 1 to 3:

sql
WITH ranked AS (
  SELECT
    d.name AS department,
    e.name AS employee,
    e.salary,
    DENSE_RANK() OVER (PARTITION BY e.department_id ORDER BY e.salary DESC) AS rnk
  FROM employees e
  JOIN departments d ON d.id = e.department_id
)
SELECT department, employee, salary
FROM ranked
WHERE rnk <= 3;

Picking the ranking function

For Engineering's salaries 90000, 85000, 85000, 70000, 69000:

salaryROW_NUMBERRANKDENSE_RANK
90000111
85000222
85000322
70000443
69000554

"Top three distinct salaries" is exactly DENSE_RANK. ROW_NUMBER stops after three people, and RANK skips rank 3. Either way, Eli is missing.

Always ask the interviewer which one they mean. "Top 3 employees" and "top 3 salaries" are different questions.

Without window functions

A correlated subquery counts how many distinct salaries in the same department are higher:

sql
SELECT d.name AS department, e.name AS employee, e.salary
FROM employees e
JOIN departments d ON d.id = e.department_id
WHERE (
  SELECT COUNT(DISTINCT e2.salary)
  FROM employees e2
  WHERE e2.department_id = e.department_id AND e2.salary > e.salary
) < 3;

It's correct but runs the subquery once per employee, O(n²) per department. It's worth knowing for databases without window functions, like MySQL before 8.0.

Reference solution

sql
WITH ranked AS (
  SELECT
    d.name AS department,
    e.name AS employee,
    e.salary,
    DENSE_RANK() OVER (PARTITION BY e.department_id ORDER BY e.salary DESC) AS rnk
  FROM employees e
  JOIN departments d ON d.id = e.department_id
)
SELECT department, employee, salary
FROM ranked
WHERE rnk <= 3;