TOPIC 24/3015 MINNOT STARTED

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.

  1. Choose the group
  2. Choose the order
  3. Pick the function
  4. Filter in an outer query
  5. Check the ties
Ranking with a tie

HOW THE PICTURE MAPS

How each part of the picture matches the work
In the pictureIn the work
A game leaderboardA ranking function
One leaderboard for each teamA partition
Two players on the same scoreA tie
Giving every player a different placeROW_NUMBER
Skipping a place after a tie, or not skippingRANK, 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.

What each part of the trick stands for
In the trickWhat it stands for
ROW_NUMBERGiving every player a different place
RANKSkipping a place after a tie
DENSE_RANKNot 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
  1. 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.

    §9.22 rank, ROW_NUMBER bigint, LAG/LEAD. PostgreSQL18 implementation, not a universal SQL mandate. · Checked 2026-11-06

    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.

  2. 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.

    SELECT: WHERE/HAVING/output aliases, functional dependence, NULL ordering and LIMIT. PostgreSQL18 implementation, not a universal SQL mandate. · Checked 2026-11-06

    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.

  3. Simplified educational illustration

    Payments Signal editorial teaching models — Payments Signal

    This site's own simplified teaching models. · Checked 2026-07-12

    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.