← Back to SQL, page 6

sql-0119 · Medium mid

Write a query that returns the two highest-paid employees in each department.

Number rows within each department by salary with ROW_NUMBER() OVER (PARTITION BY dept_id ORDER BY salary DESC, id), then keep rows numbered 1 or 2 in an outer query. Use DENSE_RANK instead if everyone tied at second place should be included.

Full answer

"Top N per group" is a window-function classic. The window does the ranking; an outer query does the filtering, because window functions cannot appear in WHERE.

sql
CREATE TABLE departments (
  id   INTEGER PRIMARY KEY,
  name TEXT NOT NULL
);
CREATE TABLE employees (
  id         INTEGER PRIMARY KEY,
  name       TEXT NOT NULL,
  dept_id    INTEGER REFERENCES departments(id),
  manager_id INTEGER REFERENCES employees(id),
  salary     INTEGER,
  hire_date  TEXT
);
INSERT INTO departments VALUES (1,'Engineering'),(2,'Sales'),(3,'Support'),(4,'Legal');
INSERT INTO employees VALUES
  (1,'Asha', 1,NULL,120000,'2019-03-01'),
  (2,'Ravi', 1,1,    95000,'2020-07-15'),
  (3,'Mei',  1,1,    95000,'2021-01-10'),
  (4,'Tom',  2,1,    70000,'2022-05-20'),
  (5,'Sara', 2,4,    62000,'2023-02-01'),
  (6,'Diego',3,1,    55000,'2021-11-30'),
  (7,'Lena', NULL,4, NULL,  '2024-06-03'),
  (8,'Omar', 3,6,    48000,'2024-09-09');
sql
WITH ranked AS (
  SELECT name, dept_id, salary,
         ROW_NUMBER() OVER (PARTITION BY dept_id ORDER BY salary DESC, name) AS rn
  FROM employees
  WHERE dept_id IS NOT NULL
)
SELECT dept_id, name, salary FROM ranked WHERE rn <= 2 ORDER BY dept_id, rn;
dept_idnamesalary
1Asha120000
1Mei95000
2Tom70000
2Sara62000
3Diego55000
3Omar48000

Engineering has three people, so Ravi (95000) is cut, even though he earns the same as Mei. That is ROW_NUMBER doing exactly what it promises: two rows per department, ties broken by name.

If ties should all be kept, rank instead:

sql
WITH ranked AS (
  SELECT name, dept_id, salary,
         DENSE_RANK() OVER (PARTITION BY dept_id ORDER BY salary DESC) AS rnk
  FROM employees
  WHERE dept_id IS NOT NULL
)
SELECT dept_id, name, salary, rnk FROM ranked WHERE rnk <= 2 ORDER BY dept_id, rnk, name;
dept_idnamesalaryrnk
1Asha1200001
1Mei950002
1Ravi950002
2Tom700001
2Sara620002
3Diego550001
3Omar480002

Now Engineering returns all three, because Ravi and Mei share rank 2. Sales and Support have only two people each, so both versions return them.

An index on (dept_id, salary DESC) helps engines that can read each department's top rows directly. Without window functions, the same result needs a correlated count ("fewer than 2 colleagues in the same department earn more"), which is harder to read.

sql
CREATE TABLE departments (
  id   INTEGER PRIMARY KEY,
  name TEXT NOT NULL
);
CREATE TABLE employees (
  id         INTEGER PRIMARY KEY,
  name       TEXT NOT NULL,
  dept_id    INTEGER REFERENCES departments(id),
  manager_id INTEGER REFERENCES employees(id),
  salary     INTEGER,
  hire_date  TEXT
);
INSERT INTO departments VALUES (1,'Engineering'),(2,'Sales'),(3,'Support'),(4,'Legal');
INSERT INTO employees VALUES
  (1,'Asha', 1,NULL,120000,'2019-03-01'),
  (2,'Ravi', 1,1,    95000,'2020-07-15'),
  (3,'Mei',  1,1,    95000,'2021-01-10'),
  (4,'Tom',  2,1,    70000,'2022-05-20'),
  (5,'Sara', 2,4,    62000,'2023-02-01'),
  (6,'Diego',3,1,    55000,'2021-11-30'),
  (7,'Lena', NULL,4, NULL,  '2024-06-03'),
  (8,'Omar', 3,6,    48000,'2024-09-09');

WITH ranked AS (
  SELECT name, dept_id, salary,
         ROW_NUMBER() OVER (PARTITION BY dept_id ORDER BY salary DESC, name) AS rn
  FROM employees
  WHERE dept_id IS NOT NULL
)
SELECT dept_id, name, salary FROM ranked WHERE rn <= 2 ORDER BY dept_id, rn;

WITH ranked AS (
  SELECT name, dept_id, salary,
         DENSE_RANK() OVER (PARTITION BY dept_id ORDER BY salary DESC) AS rnk
  FROM employees
  WHERE dept_id IS NOT NULL
)
SELECT dept_id, name, salary, rnk FROM ranked WHERE rnk <= 2 ORDER BY dept_id, rnk, name;

Checked on SQLite 3.46.1, run twice with identical output.

Key points

  • Rank inside each group with PARTITION BY
  • Filter the rank in an outer query or CTE
  • ROW_NUMBER gives exactly N per group
  • DENSE_RANK keeps all ties at the cut-off

Common mistakes

  • Filtering the window function in WHERE
  • Using ROW_NUMBER and silently dropping a tied employee

Follow-ups

  • How would you write this without window functions?
  • How would you get the top 2 per department by hire date instead?

Tips

  • Ask whether ties should be included before choosing the function