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.
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;| dept_id | name | salary |
|---|---|---|
| 1 | Asha | 120000 |
| 1 | Mei | 95000 |
| 2 | Tom | 70000 |
| 2 | Sara | 62000 |
| 3 | Diego | 55000 |
| 3 | Omar | 48000 |
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:
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_id | name | salary | rnk |
|---|---|---|---|
| 1 | Asha | 120000 | 1 |
| 1 | Mei | 95000 | 2 |
| 1 | Ravi | 95000 | 2 |
| 2 | Tom | 70000 | 1 |
| 2 | Sara | 62000 | 2 |
| 3 | Diego | 55000 | 1 |
| 3 | Omar | 48000 | 2 |
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.
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