window frames
ROWS, RANGE and GROUPS. the default frame that makes last_value look broken.
- address
- everything.analytics.window_frames
- part
- part 4, windows, grouping and recursion
- chapter
- chapter 7 of 11
- reading time
- 11 min
a window frame is the slice of the partition a window function reads for one row. it moves with the current row. ROWS counts rows, RANGE measures the ORDER BY value, GROUPS counts runs of tied rows.
write only ORDER BY and you get RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW: from the first row to the current row's last tie. that one default is why last_value looks broken.
the picture
you know how last_value(name) OVER (ORDER BY salary) is supposed to give you the best-paid person, and instead hands every row back its own name, more or less? kinda cursed. it stops being cursed once you can see the frame.
think of a ruler laid down the list of rows, with a sliding clip on it. for every row, postgres moves the clip, and the window function reads only what's inside the clip (the frame). a running total clips from the top down to here. a moving average clips one above and one below. last_value reads the last thing inside the clip, not the last thing on the ruler.
the rows here are the nine leads under one director, in salary order, with their ties. move the current row, switch the mode, and watch the bracket:
- 1SELECT id, name, salary,
- 2 array_agg(id) OVER w AS frame,
- 3 sum(salary) OVER w AS sum,
- 4 last_value(name) OVER w AS last_value
- 5FROM leads
- 6WINDOW w AS (ORDER BY salary
- 7 RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW)
- 8ORDER BY salary, id;
| id | name | salary | sum | last_value |
|---|---|---|---|---|
| 41 | Olivia Nguyen | 95000.00 | 285000.00 | Omar Kim |
| 44 | Rahul Haddad | 95000.00 | 285000.00 | Omar Kim |
| 48 | Omar Kim | 95000.00 | 285000.00 | Omar Kim |
| 45 | Chen Ahmed | 97000.00 | 382000.00 | Chen Ahmed |
| 46 | Lucas Ahmed | 98000.00 | 578000.00 | Emma Smith |
| 47 | Emma Smith | 98000.00 | 578000.00 | Emma Smith |
| 42 | Ananya Smith | 99000.00 | 875000.00 | Lucas Haddad |
| 43 | Lucas Novak | 99000.00 | 875000.00 | Lucas Haddad |
| 49 | Lucas Haddad | 99000.00 | 875000.00 | Lucas Haddad |
row 41: 3 rows in the frame (41, 44, 48); it has 2 peers. RANGE goes by value: down to this row and every row with the same salary. this is the default frame, the one you get by writing only ORDER BY.
sum and last_value for the current row, tinted live. a darker line separates salaries, so you can see the ties. switch ROWS to RANGE on the first row: the frame grows to all three people on 95,000.notice what never happens: no row disappears. nine rows go in, nine come out, each with its own sum and its own last_value. a frame only decides what a row reads. (window functions has the part about not collapsing.)
the frame clause
the frame lives at the end of the window definition, after ORDER BY:
{ ROWS | RANGE | GROUPS } BETWEEN start AND end [ EXCLUDE ... ]
start, end: UNBOUNDED PRECEDING | n PRECEDING | CURRENT ROW | n FOLLOWING | UNBOUNDED FOLLOWINGthe figure works on a small table of its own, the nine leads copied out of employees in salary order. every block below runs after this one:
CREATE TABLE leads AS
SELECT id, name, salary FROM employees
WHERE manager_id IN (14, 15, 16)
ORDER BY salary, id;
SELECT id, name, salary FROM leads ORDER BY salary, id;| idint4 | nametext | salarynumeric |
|---|---|---|
| 41 | Olivia Nguyen | 95000.00 |
| 44 | Rahul Haddad | 95000.00 |
| 48 | Omar Kim | 95000.00 |
| 45 | Chen Ahmed | 97000.00 |
| 46 | Lucas Ahmed | 98000.00 |
| 47 | Emma Smith | 98000.00 |
| 42 | Ananya Smith | 99000.00 |
| 43 | Lucas Novak | 99000.00 |
| 49 | Lucas Haddad | 99000.00 |
three salaries are tied at 95,000, two at 98,000, three at 99,000. rows that tie on the window's ORDER BY are peers, and peers are where the three modes part ways.
three ways to say "1 PRECEDING"
the bounds read the same in every mode. what "1" counts is the difference:
- ROWS: one row. the row above, whatever it holds.
- RANGE: one unit of the ORDER BY value.
RANGE BETWEEN 1000 PRECEDING AND 1000 FOLLOWINGis "everyone paid within 1,000 of me". it needs exactly one ORDER BY column, of a type you can subtract from (numbers, dates with an interval). - GROUPS: one peer group. the whole run of tied rows above, however long it is.
and CURRENT ROW changes too: in ROWS it's this row, in RANGE and GROUPS it's this row and its peers.
-- for Chen Ahmed, the only lead on 97,000
SELECT count(*) OVER (ORDER BY salary
GROUPS BETWEEN 1 PRECEDING AND 1 FOLLOWING)
FROM leads;all three at once, one window per mode:
SELECT id, salary,
count(*) OVER (ORDER BY salary, id ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING) AS by_rows,
count(*) OVER (ORDER BY salary RANGE BETWEEN 1000 PRECEDING AND 1000 FOLLOWING) AS by_range,
count(*) OVER (ORDER BY salary GROUPS BETWEEN 1 PRECEDING AND 1 FOLLOWING) AS by_groups
FROM leads
ORDER BY salary, id;| idint4 | salarynumeric | by_rowsint8 | by_rangeint8 | by_groupsint8 |
|---|---|---|---|---|
| 41 | 95000.00 | 2 | 3 | 4 |
| 44 | 95000.00 | 3 | 3 | 4 |
| 48 | 95000.00 | 3 | 3 | 4 |
| 45 | 97000.00 | 3 | 3 | 6 |
| 46 | 98000.00 | 3 | 6 | 6 |
| 47 | 98000.00 | 3 | 6 | 6 |
| 42 | 99000.00 | 3 | 5 | 5 |
| 43 | 99000.00 | 3 | 5 | 5 |
| 49 | 99000.00 | 2 | 5 | 5 |
Chen Ahmed (45) gets 3, 3 and 6. the ROWS window says ORDER BY salary, id on purpose: ROWS picks rows by position, and among ties the position is whatever the sort left. add a unique column and the positions are fixed. (RANGE and GROUPS take whole peer groups, so they don't care.)
a moving sum across a missing day
ROWS vs RANGE stops being academic the moment your data has holes. the events table has none on 2026-04-14. here's a "three-day" moving sum of purchases both ways:
- 1WITH daily AS (
- 2 SELECT occurred_at::date AS day, count(*) FILTER (WHERE kind = 'purchase') AS purchases
- 3 FROM events
- 4 WHERE occurred_at >= '2026-04-10' AND occurred_at < '2026-04-19'
- 5 GROUP BY 1
- 6)
- 7SELECT day, purchases,
- 8 array_agg(day) OVER w AS frame,
- 9 sum(purchases) OVER w AS three_days
- 10FROM daily
- 11WINDOW w AS (ORDER BY day ROWS BETWEEN 2 PRECEDING AND CURRENT ROW)
- 12ORDER BY day;
| day | purchases | three_days |
|---|---|---|
| 2026-04-10 | 31 | 31 |
| 2026-04-11 | 16 | 47 |
| 2026-04-12 | 17 | 64 |
| 2026-04-13 | 24 | 57 |
| 2026-04-15 | 22 | 63 |
| 2026-04-16 | 16 | 62 |
| 2026-04-17 | 16 | 54 |
| 2026-04-18 | 20 | 52 |
day 2026-04-15: 3 rows in the frame (2026-04-12, 2026-04-13, 2026-04-15). ROWS 2 PRECEDING counts rows. after the missing day, the two rows before the 15th are the 12th and the 13th: a three-row frame four days wide.
interval '2 days' takes the days that are really within two days, so the missing 14th just counts as nothing.63 purchases by ROWS, 46 by RANGE, for the same day. ROWS counts rows, and a missing day is a missing row, so ROWS quietly reaches further back. if the window is about time, say it in time: RANGE with an interval. (or fill the hole first with a calendar from generate_series, and then ROWS is honest again.)
the default frame, and last_value
if you write no frame clause, you still get one. which one depends on ORDER BY:
- no ORDER BY in the window: every row is a peer of every other, so the frame is the whole partition.
- ORDER BY:
RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW. from the first row to the current row's last peer.
that's what makes a running total come out of sum(x) OVER (ORDER BY day) with no frame in sight (running totals). and it's what breaks last_value.
SELECT name, salary,
last_value(name) OVER (ORDER BY salary) AS top_paid
FROM leads
ORDER BY salary, id;
-- what's top_paid on Olivia Nguyen's row (95,000, first in the list)?SELECT id, name, salary,
last_value(name) OVER (ORDER BY salary) AS top_paid
FROM leads
ORDER BY salary, id;| idint4 | nametext | salarynumeric | top_paidtext |
|---|---|---|---|
| 41 | Olivia Nguyen | 95000.00 | Omar Kim |
| 44 | Rahul Haddad | 95000.00 | Omar Kim |
| 48 | Omar Kim | 95000.00 | Omar Kim |
| 45 | Chen Ahmed | 97000.00 | Chen Ahmed |
| 46 | Lucas Ahmed | 98000.00 | Emma Smith |
| 47 | Emma Smith | 98000.00 | Emma Smith |
| 42 | Ananya Smith | 99000.00 | Lucas Haddad |
| 43 | Lucas Novak | 99000.00 | Lucas Haddad |
| 49 | Lucas Haddad | 99000.00 | Lucas Haddad |
Omar Kim three times, then Chen Ahmed's own name, then Emma Smith twice. every row reports the last of its own ties. the fix is to say which frame you meant: the whole partition.
SELECT id, name, salary,
last_value(name) OVER (ORDER BY salary, id
ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) AS top_paid,
first_value(name) OVER (ORDER BY salary DESC, id DESC) AS also_top_paid
FROM leads
ORDER BY salary, id;| idint4 | nametext | salarynumeric | top_paidtext | also_top_paidtext |
|---|---|---|---|---|
| 41 | Olivia Nguyen | 95000.00 | Lucas Haddad | Lucas Haddad |
| 44 | Rahul Haddad | 95000.00 | Lucas Haddad | Lucas Haddad |
| 48 | Omar Kim | 95000.00 | Lucas Haddad | Lucas Haddad |
| 45 | Chen Ahmed | 97000.00 | Lucas Haddad | Lucas Haddad |
| 46 | Lucas Ahmed | 98000.00 | Lucas Haddad | Lucas Haddad |
| 47 | Emma Smith | 98000.00 | Lucas Haddad | Lucas Haddad |
| 42 | Ananya Smith | 99000.00 | Lucas Haddad | Lucas Haddad |
| 43 | Lucas Novak | 99000.00 | Lucas Haddad | Lucas Haddad |
| 49 | Lucas Haddad | 99000.00 | Lucas Haddad | Lucas Haddad |
two ways to the same answer. widen the frame, or flip the order and use first_value, which works with the default frame because the first row is always in it. either way, notice the id in the ORDER BY. the top salary is a three-way tie, and "the last of them" is only one person once you've said how to break the tie. (ranking is all about that.)
EXCLUDE: leaving rows out
the last part of a frame clause takes rows back out of the frame, after the bounds have picked it. "how does my salary compare with everyone else's?" wants the whole team minus me:
- 1SELECT id, name, salary,
- 2 array_agg(id) OVER w AS frame,
- 3 count(*) OVER w AS n,
- 4 round(avg(salary) OVER w) AS avg
- 5FROM leads
- 6WINDOW w AS (ORDER BY salary
- 7 ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING
- 8 EXCLUDE CURRENT ROW)
- 9ORDER BY salary, id;
| id | name | salary | n | avg |
|---|---|---|---|---|
| 41 | Olivia Nguyen | 95000.00 | 8 | 97500 |
| 44 | Rahul Haddad | 95000.00 | 8 | 97500 |
| 48 | Omar Kim | 95000.00 | 8 | 97500 |
| 45 | Chen Ahmed | 97000.00 | 8 | 97250 |
| 46 | Lucas Ahmed | 98000.00 | 8 | 97125 |
| 47 | Emma Smith | 98000.00 | 8 | 97125 |
| 42 | Ananya Smith | 99000.00 | 8 | 97000 |
| 43 | Lucas Novak | 99000.00 | 8 | 97000 |
| 49 | Lucas Haddad | 99000.00 | 8 | 97000 |
row 41: 8 rows in the frame (44, 48, 45, 46, 47, 42, 43, 49), 1 excluded; it has 2 peers. EXCLUDE CURRENT ROW: everyone but this row. the average of everybody else.
count and avg.EXCLUDE CURRENT ROW: everyone but me. 8 rows for everyone.EXCLUDE GROUP: me and my peers. for Olivia that's 6 rows: the people not on 95,000.EXCLUDE TIES: my peers, but not me. 7 rows for Olivia.EXCLUDE NO OTHERS: the default. nothing excluded.
SELECT id, name, salary,
salary - round(avg(salary) OVER (ORDER BY salary
ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING
EXCLUDE CURRENT ROW)) AS vs_the_others
FROM leads
ORDER BY salary, id;| idint4 | nametext | salarynumeric | vs_the_othersnumeric |
|---|---|---|---|
| 41 | Olivia Nguyen | 95000.00 | -2500.00 |
| 44 | Rahul Haddad | 95000.00 | -2500.00 |
| 48 | Omar Kim | 95000.00 | -2500.00 |
| 45 | Chen Ahmed | 97000.00 | -250.00 |
| 46 | Lucas Ahmed | 98000.00 | 875.00 |
| 47 | Emma Smith | 98000.00 | 875.00 |
| 42 | Ananya Smith | 99000.00 | 2000.00 |
| 43 | Lucas Novak | 99000.00 | 2000.00 |
| 49 | Lucas Haddad | 99000.00 | 2000.00 |
Olivia earns 2,500 less than the other eight do on average. (without EXCLUDE, she'd be compared with an average that includes her own salary, which is a small lie that grows as the team shrinks.)
the rules postgres checks
a few frames don't make sense, and postgres says so before it reads a row. RANGE with an offset needs exactly one ORDER BY column to measure along:
SELECT id, sum(salary) OVER (ORDER BY salary, id
RANGE BETWEEN 1000 PRECEDING AND CURRENT ROW)
FROM leads;and a frame can't end before it starts (BETWEEN CURRENT ROW AND 1 PRECEDING), can't start at UNBOUNDED FOLLOWING, and the offsets can't be negative or NULL.
under the hood, each distinct window becomes a WindowAgg node over a sort. postgres 18's EXPLAIN prints the window definition on the node, frame included (release notes: "details about window function arguments"):
EXPLAIN (COSTS OFF)
SELECT id, sum(salary) OVER w
FROM leads
WINDOW w AS (ORDER BY salary ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING)
ORDER BY salary, id;the sort by salary feeds the WindowAgg, and the final ORDER BY only has to add id within each salary: an incremental sort, with Presorted Key: salary. (sorting has how that works.)
one level deeperwhere GROUPS and EXCLUDE came from
the SQL:2003 standard had ROWS and RANGE. SQL:2011 added GROUPS, frame exclusion, and RANGE with a distance like 1000 PRECEDING. postgres had ROWS, and RANGE with only UNBOUNDED and CURRENT ROW, until postgres 11, whose release notes list all three as "all window function framing options shown in the SQL:2011 standard".
keylines
- a frame is the slice of the partition a window function reads for the current row. it moves; nothing collapses.
- ROWS counts rows, RANGE measures the ORDER BY value, GROUPS counts runs of ties.
- the default with ORDER BY is RANGE UNBOUNDED PRECEDING to CURRENT ROW, which includes the current row's peers.
- that default is why last_value looks broken. say UNBOUNDED FOLLOWING, or use first_value backwards.
- for time windows over data with holes, use RANGE with an interval, not ROWS.