SQL for BAs: window functions and ranking
Rank rows inside groups, pick the top row per group, and decide in advance how ties are treated, using ROW_NUMBER, RANK and DENSE_RANK.
L1–L4 are part of the Pro pass
L0 Explain simply
Analogy: a leaderboard that decides a tie. Two players score the same. Do they both come first? Does the next player come second, or third? The board needs the rule before the game ends.
Daily List is a made-up to-do app (SYNTHETIC / TRAINING ONLY). Nina, the , asks Sam for the soonest open task for each person.
What it is: (Structured Query Language) can number rows without collapsing them. A table is a grid, a row is one record, and a column is one fact. A works across a set of related rows and still returns one result on every row. A ranking function is the window function that gives each row a place in order. ROW_NUMBER gives every row its own number. RANK shares a number and then skips. DENSE_RANK shares a number and does not skip. GROUP BY is the neighbour: it collapses the pile into one row, and a window keeps the rows.
Why it matters: Four open tasks share the due date 2026-10-20. A rule that keeps one row and a rule that keeps every tie do not return the same list. Nina has to pick before Sam hides a task.
A rank is only as sensible as the order behind it. A blank date will win if you let it sort first.
PLAIN WORDS · 1 in this section
- leaderboard
- A list that shows who is first, second and third.
PICTURE IT
The picture is the one the lesson above opens with. The table below shows how each part of it matches the work.
THE REAL JOB The picture helps a newcomer remember that the three ranking functions differ only in how they treat a tie.
- Choose the group
- Choose the order
- Pick the function
- Filter in an outer query
- Check the ties
HOW THE PICTURE MAPS
| In the picture | In the work |
|---|---|
| A game leaderboard | A ranking function |
| One leaderboard for each team | A partition |
| Two players on the same score | A tie |
| Giving every player a different place | ROW_NUMBER |
| Skipping a place after a tie, or not skipping | RANK, or DENSE_RANK |
WHERE THIS PICTURE STOPS
- A game ends with one winner, but a query must say how ties are treated.
- A leaderboard is on show for all. The rank in a query exists only when the query runs.
- Players do not change their own score. Data rows can change between runs.
HOOK
Same score, same place, then skip or not.
REMEMBER IT THIS WAY
A contrast: two neighbours told apart by one test.
Ask what a tie does to the next place. ROW_NUMBER gives every row its own place, RANK skips a place after a tie, DENSE_RANK does not skip.
| In the trick | What it stands for |
|---|---|
| ROW_NUMBER | Giving every player a different place |
| RANK | Skipping a place after a tie |
| DENSE_RANK | Not skipping a place after a tie |
TELL A FRIEND
A numbers rows in order inside a group, and the three differ only in how they treat ties. The window keeps every row.
OFTEN MIXED UP WITH SQL for BAs: GROUP BY and counts. That topic collapses rows into group totals. This one keeps every row and ranks it inside its group.
PRACTISE AND KEEP
Sources for this topic — 3 sources
- Market practice
Window Functions (PostgreSQL function reference) ↗ — PostgreSQL Global Development Group · PostgreSQL 18, §9.22 rank, ROW_NUMBER returns bigint, LAG/LEAD; publisher page opened6November 2026. Dialect scope explicit.
PostgreSQL18 publisher documentation opened on6November2026: §9.22 rank, ROW_NUMBER bigint, LAG/LEAD. No execution on PostgreSQL is claimed; engine-specific examples remain scoped to18.
- Market practice
SELECT command reference (PostgreSQL documentation) ↗ — PostgreSQL · PostgreSQL 18, SELECT: WHERE/HAVING/output aliases, functional dependence, NULL ordering and LIMIT; publisher page opened6November 2026. Dialect scope explicit.
PostgreSQL18 publisher documentation opened on6November2026: SELECT: WHERE/HAVING/output aliases, functional dependence, NULL ordering and LIMIT. No execution on PostgreSQL is claimed; engine-specific examples remain scoped to18.
- Simplified educational illustration
Payments Signal editorial teaching models — Payments Signal
What this simplifies: Daily List people, data and exercises are invented for teaching. Query results belong to the declared fixture and database version. Authorization, business identity, time bounds and unknown-value policy must be agreed in context; no universal mandatory query template is implied.
Used wherever diagrams, scenarios, figures, or example values are didactic constructions rather than sourced facts; every such use carries a simplifications disclosure. All people, companies, banks, and list entries in examples are fictional.
Deepest material on this page: L4 — Standards & sources. Where a topic stops short of implementation depth, that is a deliberate coverage decision, not an oversight — see coverage.