Window functions in depth: frames, lag and lead, and the latest row
Many reports show a running total, a change since yesterday or the latest item for each user. Learn how a frame of rows works so you can build data that tests them.
L1–L4 are part of the Pro pass
L0 Explain simply
Analogy: a train timetable. At each stop you can see the stop before and the stop after. The line of stops does not change as you ride. The lit group of stops is the frame you are allowed to count.
Daily List is a to-do list app. A report shows tasks done each day, and the change since the day before. Tom tests the first day, where there is no day before. (SYNTHETIC / TRAINING ONLY: Daily List and the people here are invented.).
What it is: A is a (Structured Query Language) calculation that looks across rows without collapsing them into one total. SQL asks questions of a database. A table is a grid. A row is one day or one task. A column is one fact, such as the count for that day. LAG reads a previous row. LEAD reads a next row. The neighbour is GROUP BY, which folds many rows into one. A window keeps every row and adds a new column.
Why it matters: The first day has no previous day. A report that shows a change of 5 there has treated 'no day' as zero. Two tasks with the same time can also make 'the latest task' flip between titles.
A is the set the running total may use. A comes from LAG and LEAD. The latest row per group is one row kept from each user.
PICTURE IT
The picture is the one the lesson above opens with. The table below shows how each part of it matches the software.
THE REAL JOB A train timetable with a stop before and a stop after.
- Build
- Run
- Compare
HOW THE PICTURE MAPS
| In the picture | In the software |
|---|---|
| The stops in view | The frame |
| The previous stop | LAG |
| The next stop | LEAD |
| The last stop on each line | Latest row per group |
WHERE THIS PICTURE STOPS
- A timetable is fixed, but a frame counts rows.
- A line is drawn, but a window is a query idea.
- Stops have a route order, but a window needs ORDER BY.
HOOK
Test the first dancer.
TELL A FRIEND
A timetable: test the first stop, where nothing is before.
OFTEN MIXED UP WITH Subqueries and window functions: find the odd row out. That topic introduces subqueries and windows. This one covers frames, lag and lead, and the latest row, with edge data.
PRACTISE AND KEEP
Sources for this topic — 4 sources
- Market practice
Window functions (PostgreSQL tutorial) ↗ — PostgreSQL · PostgreSQL tutorial: partition by splits rows into groups, order by sets the order inside each group, and with an order the default frame runs from the start of the group to the current row plus any ties (search excerpt, page not opened)
Found through a web search; the page was not opened (search excerpt, page not opened). Version: Web page, no edition number. Licence terms were not read, so put it in your own words and do not quote it. Open the page and confirm what it says before relying on any detail.
- Market practice
Window functions reference (PostgreSQL documentation) ↗ — PostgreSQL · PostgreSQL reference: lag looks back and lead looks forward by an offset that defaults to one, and a window function cannot be used in where so a latest-row filter needs a subquery (search excerpt, page not opened)
Found through a web search; the page was not opened (search excerpt, page not opened). Version: Web page, no edition number. Licence terms were not read, so put it in your own words and do not quote it. Open the page and confirm what it says before relying on any detail.
- Implementation-specific
PostgreSQL documentation ↗ — The PostgreSQL Global Development Group · PostgreSQL 18 official manual, opened 2026-11-06: functions-window §9.22 and tutorial-window. Supports only PostgreSQL behavior; earlier execution evidence remains separately versioned.
An implementation, not the standard — which is the point. ISO/IEC 9075-2 is paywalled, and this documents the same constructs in text anybody can open. Its isolation-level chapter attributes to the standard explicitly, so it can carry a standard-level claim as long as the citation says which document it is quoting.
Cited as market practice rather than an official standard by kind: an engine's manual is authoritative about that engine and is not the obligation. Where a question turns on behaviour PostgreSQL chose rather than behaviour SQL requires, the answer has to say so.
One correction carried here because it circulates widely: the sentence "The fundamental difference between WHERE and HAVING…", quoted all over the internet as PostgreSQL documentation, is not in the current documentation. It was searched for in the source tree and is not there.
Read through a mirror, because the publisher's own domain could not be fetched (a reachability gap, not a statement about the publisher). A 403 there is a policy refusal and says nothing about whether the document exists or is reachable elsewhere.
Anyone on an unrestricted network should open the canonical URL above and confirm the section and page numbers cited against it. Every claim drawn from this entry is written so that it can be checked against a numbered page.
Read from the
postgres/postgresdocumentation source tree (doc/src/sgml), which the project itself maintains and which carries the section ids the published URLs are derived from — so a cited section id can be checked against the rendered page. - Implementation-specific
SQLite official documentation — constraints, types and transactions ↗ — SQLite · SQLite rolling official documentation, opened 2026-11-06: foreignkeys.html, datatype3.html, lang_createtable.html, lang_transaction.html, isolation.html, cli.html, series.html and lang_createtrigger.html as relevant. Historical SQLite 3.40.1 results remain distinct from in-memory audit witnesses on 3.53.1.
Relevant primary pages opened 6 November2026; rolling documentation and CLI examples (3.54.0) are distinct from historical lesson runs3.40.1 and audit witnesses3.53.1. No file/connected database audit execution.
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.