Top three salaries in each department
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
| id | name |
|---|---|
| 1 | Engineering |
| 2 | Sales |
employees
| id | name | salary | department_id |
|---|---|---|---|
| 1 | Noa | 85000 | 1 |
| 2 | Avi | 60000 | 1 |
| 3 | Dana | 85000 | 1 |
| 4 | Yossi | 90000 | 1 |
| 5 | Maya | 69000 | 1 |
| 6 | Eli | 70000 | 1 |
| 7 | Tal | 80000 | 2 |
| 8 | Roni | 60000 | 2 |
Result:
| department | employee | salary |
|---|---|---|
| Engineering | Yossi | 90000 |
| Engineering | Noa | 85000 |
| Engineering | Dana | 85000 |
| Engineering | Eli | 70000 |
| Sales | Tal | 80000 |
| Sales | Roni | 60000 |
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
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:
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:
| salary | ROW_NUMBER | RANK | DENSE_RANK |
|---|---|---|---|
| 90000 | 1 | 1 | 1 |
| 85000 | 2 | 2 | 2 |
| 85000 | 3 | 2 | 2 |
| 70000 | 4 | 4 | 3 |
| 69000 | 5 | 5 | 4 |
"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:
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
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;