skip to content

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:

mode =
frame =
query.sql
  1. 1SELECT id, name, salary,
  2. 2 array_agg(id) OVER w AS frame,
  3. 3 sum(salary) OVER w AS sum,
  4. 4 last_value(name) OVER w AS last_value
  5. 5FROM leads
  6. 6WINDOW w AS (ORDER BY salary
  7. 7 RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW)
  8. 8ORDER BY salary, id;
the rows of the window, with the frame of the current row marked
idnamesalarysumlast_value
41Olivia Nguyen95000.00285000.00Omar Kim
44Rahul Haddad95000.00285000.00Omar Kim
48Omar Kim95000.00285000.00Omar Kim
45Chen Ahmed97000.00382000.00Chen Ahmed
46Lucas Ahmed98000.00578000.00Emma Smith
47Emma Smith98000.00578000.00Emma Smith
42Ananya Smith99000.00875000.00Lucas Haddad
43Lucas Novak99000.00875000.00Lucas Haddad
49Lucas Haddad99000.00875000.00Lucas Haddad
(9 rows in, 9 rows out)
current rowin the frame

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.

row 1/9
real output, postgres 18.6 (PGlite 0.5.12) · window-frames
click a row, or step through them. the bracket is the frame, the rows that fed 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:

text
{ ROWS | RANGE | GROUPS } BETWEEN start AND end [ EXCLUDE ... ]
 
start, end:  UNBOUNDED PRECEDING | n PRECEDING | CURRENT ROW | n FOLLOWING | UNBOUNDED FOLLOWING

the 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:

sqldb=org · session leads
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;
output, from postgres 18.6
#1 · SELECT 9
#2 · SELECT 9
idint4nametextsalarynumeric
41Olivia Nguyen95000.00
44Rahul Haddad95000.00
48Omar Kim95000.00
45Chen Ahmed97000.00
46Lucas Ahmed98000.00
47Emma Smith98000.00
42Ananya Smith99000.00
43Lucas Novak99000.00
49Lucas Haddad99000.00
(9 rows)

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 FOLLOWING is "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.

predict the output
sql
-- 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:

sqldb=org · session leads
continues session leads (1 earlier block run first)
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;
output, from postgres 18.6
SELECT 9
idint4salarynumericby_rowsint8by_rangeint8by_groupsint8
4195000.00234
4495000.00334
4895000.00334
4597000.00336
4698000.00366
4798000.00366
4299000.00355
4399000.00355
4999000.00255
(9 rows)

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:

mode =
query.sql
  1. 1WITH daily AS (
  2. 2 SELECT occurred_at::date AS day, count(*) FILTER (WHERE kind = 'purchase') AS purchases
  3. 3 FROM events
  4. 4 WHERE occurred_at >= '2026-04-10' AND occurred_at < '2026-04-19'
  5. 5 GROUP BY 1
  6. 6)
  7. 7SELECT day, purchases,
  8. 8 array_agg(day) OVER w AS frame,
  9. 9 sum(purchases) OVER w AS three_days
  10. 10FROM daily
  11. 11WINDOW w AS (ORDER BY day ROWS BETWEEN 2 PRECEDING AND CURRENT ROW)
  12. 12ORDER BY day;
the rows of the window, with the frame of the current row marked
daypurchasesthree_days
2026-04-103131
2026-04-111647
2026-04-121764
2026-04-132457
2026-04-152263
2026-04-161662
2026-04-171654
2026-04-182052
(8 rows in, 8 rows out)
current rowin the frame

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.

day 5/8
real output, postgres 18.6 (PGlite 0.5.12) · window-frames-days
step to the 15th. ROWS 2 PRECEDING takes the two rows above, the 12th and the 13th: three rows spanning four days. RANGE 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.

predict the output
sql
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)?
sqldb=org · session leads
continues session leads (2 earlier blocks run first)
SELECT id, name, salary,
       last_value(name) OVER (ORDER BY salary) AS top_paid
FROM leads
ORDER BY salary, id;
output, from postgres 18.6
SELECT 9
idint4nametextsalarynumerictop_paidtext
41Olivia Nguyen95000.00Omar Kim
44Rahul Haddad95000.00Omar Kim
48Omar Kim95000.00Omar Kim
45Chen Ahmed97000.00Chen Ahmed
46Lucas Ahmed98000.00Emma Smith
47Emma Smith98000.00Emma Smith
42Ananya Smith99000.00Lucas Haddad
43Lucas Novak99000.00Lucas Haddad
49Lucas Haddad99000.00Lucas Haddad
(9 rows)

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.

sqldb=org · session leads
continues session leads (3 earlier blocks run first)
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;
output, from postgres 18.6
SELECT 9
idint4nametextsalarynumerictop_paidtextalso_top_paidtext
41Olivia Nguyen95000.00Lucas HaddadLucas Haddad
44Rahul Haddad95000.00Lucas HaddadLucas Haddad
48Omar Kim95000.00Lucas HaddadLucas Haddad
45Chen Ahmed97000.00Lucas HaddadLucas Haddad
46Lucas Ahmed98000.00Lucas HaddadLucas Haddad
47Emma Smith98000.00Lucas HaddadLucas Haddad
42Ananya Smith99000.00Lucas HaddadLucas Haddad
43Lucas Novak99000.00Lucas HaddadLucas Haddad
49Lucas Haddad99000.00Lucas HaddadLucas Haddad
(9 rows)

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:

EXCLUDE =
query.sql
  1. 1SELECT id, name, salary,
  2. 2 array_agg(id) OVER w AS frame,
  3. 3 count(*) OVER w AS n,
  4. 4 round(avg(salary) OVER w) AS avg
  5. 5FROM leads
  6. 6WINDOW w AS (ORDER BY salary
  7. 7 ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING
  8. 8 EXCLUDE CURRENT ROW)
  9. 9ORDER BY salary, id;
the rows of the window, with the frame of the current row marked
idnamesalarynavg
41Olivia Nguyen95000.00897500
44Rahul Haddad95000.00897500
48Omar Kim95000.00897500
45Chen Ahmed97000.00897250
46Lucas Ahmed98000.00897125
47Emma Smith98000.00897125
42Ananya Smith99000.00897000
43Lucas Novak99000.00897000
49Lucas Haddad99000.00897000
(9 rows in, 9 rows out)
current rowin the frameexcluded

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.

row 1/9
real output, postgres 18.6 (PGlite 0.5.12) · window-frames
the frame is always the whole table here (the bracket). switch EXCLUDE and step through the rows: dashed rows are inside the bracket but left out of 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.
sqldb=org · session leads
continues session leads (5 earlier blocks run first)
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;
output, from postgres 18.6
SELECT 9
idint4nametextsalarynumericvs_the_othersnumeric
41Olivia Nguyen95000.00-2500.00
44Rahul Haddad95000.00-2500.00
48Omar Kim95000.00-2500.00
45Chen Ahmed97000.00-250.00
46Lucas Ahmed98000.00875.00
47Emma Smith98000.00875.00
42Ananya Smith99000.002000.00
43Lucas Novak99000.002000.00
49Lucas Haddad99000.002000.00
(9 rows)

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:

sqldb=org · session leads
continues session leads (6 earlier blocks run first)
SELECT id, sum(salary) OVER (ORDER BY salary, id
                             RANGE BETWEEN 1000 PRECEDING AND CURRENT ROW)
FROM leads;
output, from postgres 18.6
ERROR 42P20: RANGE with offset PRECEDING/FOLLOWING requires exactly one ORDER BY column
1 │ SELECT id, sum(salary) OVER (ORDER BY salary, id
at line 1, column 29

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"):

sqldb=org · session leads
continues session leads (7 earlier blocks run first)
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;
output, from postgres 18.6
EXPLAIN
Incremental Sort Sort Key: salary, id Presorted Key: salary -> WindowAgg Window: w AS (ORDER BY salary ROWS BETWEEN '1'::bigint PRECEDING AND '1'::bigint FOLLOWING) -> Sort Sort Key: salary -> Seq Scan on leads

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.