Window functions in depth: frames, lag and lead, and top N per group
Go past ranking. Learn what a frame of rows is, how to compare a row with the one before it, and how to keep the top few rows in each group.
L1–L4 are part of the Pro pass
L0 Explain simply
Analogy: a moving spotlight. The spotlight lights this dancer and the two behind. The dancers do not leave the stage. Only the lit group moves.
Daily List is a made-up to-do app (SYNTHETIC / TRAINING ONLY). Sam, the , wants a three-day average of tasks done. Nina, the , also wants the newest task for each user.
What it is: (Structured Query Language) is a language for asking a database questions. A table is a grid, a row is one day or one task, and a column is one fact. A says which nearby rows a window calculation may use. Lag and lead read a value from an earlier row or a later row. Top-N per group keeps the first rows inside each group, such as the newest task per user. The window adds a column and keeps every row. Its neighbour is GROUP BY, which collapses each group into one row.
Why it matters: Without a frame, a can jump when two rows tie. Without an outer filter, you cannot keep only the top row. Sam would show every task, or a cumulative value shared by tied rows rather than a row-by-row increment.
The stage has four days. A live chart has more days, and the spotlight still moves one row at a time.
PLAIN WORDS · 1 in this section
- increment
- a small step up; in Scrum, finished usable work
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 A moving spotlight on a stage.
- Order
- Frame
- Read
HOW THE PICTURE MAPS
| In the picture | In the work |
|---|---|
| The lit dancers | The frame |
| The dancer behind | LAG |
| The dancer in front | LEAD |
| The best dancer in each row | Top N per group |
WHERE THIS PICTURE STOPS
- A spotlight is smooth, but a frame counts rows.
- A stage is seen, but a window is a query concept.
- A dance has an order by music, but a window needs ORDER BY.
HOOK
Order, frame, then read.
TELL A FRIEND
A spotlight: the lit rows move, the stage stays.
OFTEN MIXED UP WITH SQL for BAs: window functions and ranking. That topic introduces ranking windows. This one covers frames, , and top N.
PRACTISE AND KEEP
Sources for this topic — 4 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
Window Functions (PostgreSQL tutorial) ↗ — PostgreSQL Global Development Group · PostgreSQL 18, §3.5 window placement, partition/order/frame; publisher page opened6November 2026. Dialect scope explicit.
PostgreSQL18 publisher documentation opened on6November2026: §3.5 window placement, partition/order/frame. No execution on PostgreSQL is claimed; engine-specific examples remain scoped to18.
- Market practice
SQLite: window functions ↗ — SQLite project · SQLite: window functions; Peer frames, ROWS/RANGE, LAG/LEAD absent-row defaults. Publisher page opened6November 2026.
Publisher documentation opened on6November2026. Engine/version scope is explicit; other database implementations may differ. Teaching queries are synthetic, not a required publisher template.
- 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.