ROW_NUMBER & Ranking in SQL
Number rows per group without collapsing them
Window functions compute a value for every row from a “window” of related rows — without grouping them away. ROW_NUMBER() OVER (PARTITION BY department ORDER BY salary DESC) numbers employees within each department, best first, while every column stays visible. GROUP BY answers “per department” questions; windows answer “for each row, compared to its group” questions.
Partition the rows
OVER (PARTITION BY department …) slices the result into per-department groups — like GROUP BY, except the rows themselves survive.
Order inside each partition
… ORDER BY salary DESC ranks employees best-first within their own department; the sort is (department, salary DESC) in one pass.
Stamp the numbers
ROW_NUMBER() walks each sorted partition assigning 1, 2, 3… — deterministic, never tied, unlike RANK().
Every column stays visible
The function adds a column; it removes none. Filter, join and display the result like any ordinary query.
Top-N per group = CTE + filter
Wrap the windowed query in a CTE, then WHERE rn <= 3 outside it — the classic “top 3 per department” interview answer.
1SELECT name, department, salary,2 ROW_NUMBER() OVER (PARTITION BY department ORDER BY salary DESC) AS rn3FROM employees ORDER BY department, rn;
| name | department | salary | rn |
|---|---|---|---|
| Cynthia | Engineering | 91000 | 1 |
| Alice | Engineering | 85000 | 2 |
| Farid | Engineering | 73000 | 3 |
| David | Marketing | 47000 | 1 |
| Elena | Sales | 58000 | 1 |
| Bob | Sales | 52000 | 2 |
Six rows in, six rows out — nothing is collapsed. Within Engineering the salaries 91k, 85k, 73k become positions 1, 2, 3; Sales numbers Elena before Bob; Marketing’s single row is 1. A WHERE rn = 1 (in a CTE) would return the three department toppers.
Number employees per department
Rank without collapsing — the window signature move.
1SELECT name, department, salary,2 ROW_NUMBER() OVER (PARTITION BY department ORDER BY salary DESC) AS rn3FROM employees4ORDER BY department, rn;
| name | department | salary | rn |
|---|---|---|---|
| Cynthia | Engineering | 91000 | 1 |
| Alice | Engineering | 85000 | 2 |
| Farid | Engineering | 73000 | 3 |
| David | Marketing | 47000 | 1 |
| Elena | Sales | 58000 | 1 |
| Bob | Sales | 52000 | 2 |
Top earner per department
The window-in-CTE-then-filter pattern.
1WITH ranked AS (2 SELECT name, department, salary,3 ROW_NUMBER() OVER (PARTITION BY department ORDER BY salary DESC) AS rn4 FROM employees5)6SELECT name, department, salary FROM ranked WHERE rn = 1;
| name | department | salary |
|---|---|---|
| Cynthia | Engineering | 91000 |
| David | Marketing | 47000 |
| Elena | Sales | 58000 |
ROW_NUMBER vs RANK vs DENSE_RANK
Same input, three tie behaviours.
1SELECT name, salary,2 ROW_NUMBER() OVER (ORDER BY salary DESC) AS rn,3 RANK() OVER (ORDER BY salary DESC) AS rk,4 DENSE_RANK() OVER (ORDER BY salary DESC) AS dr5FROM employees WHERE department = 'Engineering';
| name | salary | rn | rk | dr |
|---|---|---|---|---|
| Cynthia | 91000 | 1 | 1 | 1 |
| Alice | 85000 | 2 | 2 | 2 |
| Farid | 73000 | 3 | 3 | 3 |
Delete duplicates with ROW_NUMBER
Keep one row per business key — a production classic.
1WITH dups AS (2 SELECT id,3 ROW_NUMBER() OVER (PARTITION BY email ORDER BY id) AS rn4 FROM staff5)6DELETE FROM staff USING dups7WHERE staff.id = dups.id AND dups.rn > 1;8-- keeps the lowest id per email, removes the rest
| result |
|---|
| 2 rows deleted — one row per email remains |
Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Practice what you learned
Take the queries you just visualized and see the data structures that power real databases.