RECAP
Window functions side by side
A window function works across rows but keeps every row, adding a new column. Pick a card to see it below on the same books and members.
ROW_NUMBER() OVER (ORDER BY year) numbers every row 1, 2, 3 in year order.
| title | year | position | |
|---|---|---|---|
| Frankenstein | 1818 | 1 | |
| Dracula | 1897 | 2 | |
| Brave New World | 1932 | 3 | |
| Dune | 1965 | 4 | |
| Carrie | 1974 | 5 | |
| The Hunger Games | 2008 | 6 |
query
SELECT title, year, ROW_NUMBER() OVER (ORDER BY year) AS position FROM books;
Every row stays: GROUP BY would squash each genre into one row instead.
The three numbering functions on a tie
| name | loans | ROW_NUMBER | RANK | DENSE_RANK |
|---|---|---|---|---|
| Ava | 3 | 1 | 1 | 1 |
| Ben | 2 | 2 | 2 | 2 |
| Cara | 2 | 3 | 2 | 2 |
| Dev | 1 | 4 | 4 | 3 |
Ben and Cara tie on 2 loans. ROW_NUMBER still splits them (in no fixed order); RANK gives both 2 and skips 3; DENSE_RANK gives both 2 and carries on at 3.
GROUP BY or a window?
| SUM(pages) ... GROUP BY genre | SUM(pages) OVER (PARTITION BY genre) | |
|---|---|---|
| Rows back | One per genre | Every book |
| Can show title? | No: a group has many titles | Yes, next to its genre's total |
| Filter on it | HAVING | In a WITH step, then WHERE |
| Good for | Summaries | Ranks, running totals, "share of the total" |
Things people mix up
- WHERE can't see a window column. WHERE runs before the window is worked out: number the rows in a WITH step, then filter on the number.
- ORDER BY in OVER is not the result's order. It says how to count. Add a normal ORDER BY at the end to sort what comes back.
- No ORDER BY in OVER means no running total.
SUM(pages) OVER ()puts the grand total on every row. - Ties in a running total. Rows that tie on the ORDER BY are added together.
ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROWadds them one at a time. - In Excel, RANK.EQ is RANK, a running total is
=SUM($B$2:B2)filled down, and ROW_NUMBER per group is=COUNTIF($A$2:A2, A2). - Not every database has them. MySQL needs 8.0 or later, and Sybase ASE has none.
The lessons behind this page
Know someone fighting with a spreadsheet? Send them this page.