SQL window functions cheat sheet
Window functions on one printable page: ranking, running totals, frames for moving averages, looking at other rows with LAG and LEAD, and filtering on a window result. Every snippet runs on PostgreSQL and SQLite.
Ranking
SELECT name, age, ROW_NUMBER() OVER (ORDER BY age, name) AS rn, RANK() OVER (ORDER BY age) AS rnk, DENSE_RANK() OVER (ORDER BY age) AS drnk FROM pets;
Ties: 1 2 3 · 1 1 3 · 1 1 2.
SELECT species, name, weight_kg,
RANK() OVER (PARTITION BY species
ORDER BY weight_kg DESC) AS r
FROM pets;PARTITION BY restarts per group.
SELECT name, NTILE(4) OVER (ORDER BY weight_kg) AS quartile FROM pets;
NTILE(n): n roughly equal buckets.
SELECT name, PERCENT_RANK() OVER (ORDER BY weight_kg) AS pr FROM pets;
0 = lowest, 1 = highest.
Totals and shares
SELECT id, ordered_on, COUNT(*) OVER (ORDER BY ordered_on, id) AS running FROM orders;
Running count: ORDER BY inside OVER (add a tie-breaker).
SELECT name, weight_kg, weight_kg / SUM(weight_kg) OVER () AS share FROM pets;
OVER () = all rows: share of the total.
SELECT species, COUNT(*) AS pets, SUM(COUNT(*)) OVER () AS all_pets FROM pets GROUP BY species;
A window over grouped rows.
Frames
SELECT id, ordered_on,
AVG(id) OVER (ORDER BY id
ROWS BETWEEN 2 PRECEDING AND CURRENT ROW) AS avg_3
FROM orders;Moving average: this row and the two before.
SELECT id,
SUM(id) OVER (ORDER BY id
ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING) AS around
FROM orders;Centred frame.
SELECT name, species,
LAST_VALUE(name) OVER (PARTITION BY species ORDER BY arrived_on
ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) AS latest
FROM pets;LAST_VALUE needs the full frame, or it returns the current row.
Other rows: LAG and LEAD
SELECT id, ordered_on, LAG(ordered_on) OVER (ORDER BY id) AS previous FROM orders;
The row before (NULL for the first).
SELECT customer_id, id, LEAD(id) OVER (PARTITION BY customer_id ORDER BY id) AS next_order FROM orders;
The row after, within each group.
SELECT name, price, price - LAG(price, 1, 0) OVER (ORDER BY price) AS step FROM menu_items;
LAG(x, offset, default).
SELECT name, species, FIRST_VALUE(name) OVER (PARTITION BY species ORDER BY arrived_on) AS first_in FROM pets;
The first row of each window.
Filter on a window
SELECT species, name FROM (
SELECT species, name, ROW_NUMBER() OVER (
PARTITION BY species ORDER BY age DESC) AS rn
FROM pets) t
WHERE rn = 1;Windows run after WHERE: filter in an outer query.
WITH ranked AS ( SELECT name, RANK() OVER (ORDER BY weight_kg DESC) AS r FROM pets) SELECT name FROM ranked WHERE r <= 3;
Or with a CTE.
Window traps
SELECT name FROM pets WHERE ROW_NUMBER() OVER (ORDER BY age) = 1;
Not allowed in WHERE.
SELECT name, ROW_NUMBER() FROM pets;
Every window function needs OVER (…).
SELECT name, species, RANK() OVER w AS r FROM pets WINDOW w AS (PARTITION BY species ORDER BY age);
WINDOW names a window to reuse.
More cheat sheets
Learn it properly
Read the SQL window functions guide, or practise in “Ranking with OVER” (Pro): short exercises with instant feedback.