Code & computing / QUICK REFERENCE
PostgreSQL window functions: ties, frames and worked results
Compare ranking, row and date frames, NULL handling and previous/next values using one small PostgreSQL dataset with verified outputs.
One dataset and partition-aware totals
- Example relation
- sales(id integer, team text, day date, amount integer)
All examples use only the seven synthetic rows below. Use a disposable database for practice. [1]
- Seven synthetic input rows
- 1 | A | 2026-01-01 | 10 2 | A | 2026-01-02 | 20 3 | A | 2026-01-02 | 20 4 | A | 2026-01-04 | NULL 5 | B | 2026-01-01 | 5 6 | B | 2026-01-03 | 15 7 | C | 2026-01-02 | 7
A includes ties and NULL; C is a singleton. These cases expose frame and ranking differences. [1]
- Processing order
- FROM / WHERE / GROUP BY / HAVING → windows → final ordering
Filter a computed window result in an outer query; filtering beforehand changes which rows the window sees. [1]
- Two separate ordering jobs
- OVER (ORDER BY ...) versus final ORDER BY
The window order controls calculation. A final ORDER BY makes displayed output deterministic. [1]
- Rows in each team
SELECT id, count(*) OVER (PARTITION BY team) AS result FROM sales ORDER BY id;A has four rows, including its NULL amount. [1]
- Known amounts in each team
SELECT id, count(amount) OVER (PARTITION BY team) AS result FROM sales ORDER BY id;A has three non-NULL amounts. [3]
- Team total beside every row
SELECT id, sum(amount) OVER (PARTITION BY team) AS result FROM sales ORDER BY id;A totals 50; its four row identities remain. [1]
- Team mean rounded for display
SELECT id, round(avg(amount) OVER (PARTITION BY team), 2) AS result FROM sales ORDER BY id;A's mean excludes its NULL amount. [3]
Ranking, ties, buckets and NULL ordering
- Deterministic row numbers
SELECT id, row_number() OVER (PARTITION BY team ORDER BY amount DESC NULLS LAST, id) AS result FROM sales ORDER BY id;A by id: 3, 1, 2, 4; id breaks ties. [1]
- Competition rank with ties
SELECT id, rank() OVER (PARTITION BY team ORDER BY amount DESC NULLS LAST) AS result FROM sales ORDER BY id;A by id: 3, 1, 1, 4. [2]
- Dense rank without gaps
SELECT id, dense_rank() OVER (PARTITION BY team ORDER BY amount DESC NULLS LAST) AS result FROM sales ORDER BY id;A by id: 2, 1, 1, 3. [2]
- Relative rank including singleton
SELECT id, round(percent_rank() OVER (PARTITION BY team ORDER BY amount DESC NULLS LAST)::numeric, 3) AS result FROM sales ORDER BY id;The single C row returns 0.000. [2]
- Cumulative distribution includes peers
SELECT id, round(cume_dist() OVER (PARTITION BY team ORDER BY amount DESC NULLS LAST)::numeric, 3) AS result FROM sales ORDER BY id;Both A rows worth 20 return 0.500. [2]
- Split ordered rows into three buckets
SELECT id, ntile(3) OVER (PARTITION BY team ORDER BY id) AS result FROM sales ORDER BY id;A buckets: 1, 1, 2, 3; peers may split. [2]
- Put unknown amounts first explicitly
SELECT id, row_number() OVER (PARTITION BY team ORDER BY amount DESC NULLS FIRST, id) AS result FROM sales ORDER BY id;A's unknown amount receives row number 1. [3]
- Adding an id changes rank peers
SELECT id, rank() OVER (PARTITION BY team ORDER BY amount DESC NULLS LAST, id) AS result FROM sales ORDER BY id;Unique ids separate otherwise equal amounts. [2]
ROWS, RANGE, GROUPS and frame boundaries
- Default frame includes same-date peers
SELECT id, sum(amount) OVER (PARTITION BY team ORDER BY day) AS result FROM sales ORDER BY id;Both January 2 rows in A see total 50. [1]
- Running total one row at a time
SELECT id, sum(amount) OVER (PARTITION BY team ORDER BY day, id ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS result FROM sales ORDER BY id;Explicit ROWS and unique order give A: 10, 30, 50, 50. [3]
- Previous physical row plus current
SELECT id, sum(amount) OVER (PARTITION BY team ORDER BY day, id ROWS BETWEEN 1 PRECEDING AND CURRENT ROW) AS result FROM sales ORDER BY id;A's last frame contains 20 and NULL, summing to 20. [3]
- Previous calendar day plus current
SELECT id, sum(amount) OVER (PARTITION BY team ORDER BY day RANGE BETWEEN INTERVAL '1 day' PRECEDING AND CURRENT ROW) AS result FROM sales ORDER BY id;A's January 4 frame has only NULL; result is NULL. [3]
- Previous peer group plus current
SELECT id, sum(amount) OVER (PARTITION BY team ORDER BY day GROUPS BETWEEN 1 PRECEDING AND CURRENT ROW) AS result FROM sales ORDER BY id;A's last frame includes both January 2 rows. [3]
- Last value with the default frame
SELECT id, last_value(amount) OVER (PARTITION BY team ORDER BY day, id) AS result FROM sales ORDER BY id;With unique ordering, the default ends at the current row. [2]
- Last value across the entire team
SELECT id, last_value(amount) OVER (PARTITION BY team ORDER BY day, id ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) AS result FROM sales ORDER BY id;A ends with NULL; PostgreSQL does not skip it. [2]
- Total excluding this row
SELECT id, sum(amount) OVER (PARTITION BY team ORDER BY id ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE CURRENT ROW) AS result FROM sales ORDER BY id;C's empty frame produces NULL, not zero. [3]
Neighbors, selected rows and contributions
- Previous value with missing-row default
SELECT id, lag(amount, 1, -1) OVER (PARTITION BY team ORDER BY id) AS result FROM sales ORDER BY id;The first row in each team uses the −1 default. [2]
- Next value preserves an actual NULL
SELECT id, lead(amount, 1, -1) OVER (PARTITION BY team ORDER BY id) AS result FROM sales ORDER BY id;Id 3 sees actual NULL; id 4 uses missing-row −1. [2]
- Second value across the entire team
SELECT id, nth_value(amount, 2) OVER (PARTITION BY team ORDER BY id ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) AS result FROM sales ORDER BY id;C has no second row, so its result is NULL. [2]
- First value across the entire team
SELECT id, first_value(amount) OVER (PARTITION BY team ORDER BY id ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) AS result FROM sales ORDER BY id;A's first amount is 10 for every A row. [2]
- Difference from previous amount
SELECT id, amount - lag(amount) OVER (PARTITION BY team ORDER BY id) AS result FROM sales ORDER BY id;Unknown operands propagate NULL; first differences are unknown. [1]
- Keep one best known row per team
WITH ranked AS (SELECT id, team, amount, row_number() OVER (PARTITION BY team ORDER BY amount DESC NULLS LAST, id) AS rn FROM sales) SELECT id, team, amount FROM ranked WHERE rn = 1 ORDER BY id;The outer query keeps ids 2, 6 and 7. [1]
- Conditional team total
SELECT id, sum(amount) FILTER (WHERE amount >= 15) OVER (PARTITION BY team) AS result FROM sales ORDER BY id;A's qualifying amounts total 40; C has none. [3]
- Percent of team total
SELECT id, round(100.0 * amount / NULLIF(sum(amount) OVER (PARTITION BY team), 0), 2) AS result FROM sales ORDER BY id;Decimal arithmetic avoids integer truncation; NULLIF guards zero denominators. [1]
Reserved placement, separate from reference controls. No ad network is active.
No matching entry. Try a shorter word or clear the search to see the complete sheet.
Sources
cheatsheetatlas.com/sheets/sql-window-functions/ · Updated 2026-10-26 · PostgreSQL 18; native fixture target 18.6