Sub topic 11
The default frame and why it surprises people
Prerequisites
This chapter assumes you can write an OVER clause with PARTITION BY and ORDER BY. It also assumes you know what a frame is from the previous chapter: the set of rows, inside the partition, that an aggregate or value function looks at for the current row. Here you will see which frame the database picks when you don't write one, and why that choice produces results that look wrong.
The rule
If you leave out the frame clause, the database applies a default. The default depends on one thing: whether the window has an ORDER BY.
| Window definition | Default frame | Rows the function sees |
|---|---|---|
| `OVER (PARTITION BY region)` | The whole partition | Every row in the partition |
| `OVER (PARTITION BY region ORDER BY sale_date)` | `RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW` | From the partition start through the current row and all its peers |
A peer is a row that has the same value as the current row in the window's ORDER BY columns. If two rows share a sale_date and the window orders only by sale_date, they are peers.
The standard defines this behaviour, and PostgreSQL, MySQL 8+, SQL Server, SQLite and Oracle all follow it. Two features of the default cause nearly all the surprises. The default frame is RANGE, not ROWS. Its upper edge is the current row, not the end of the partition.
A small table with a tie
The examples below use this data. Rows 2 and 3 are in the same region and have the same date, so they are peers whenever the window orders by sale_date alone.
CREATE TABLE sales (
id INTEGER PRIMARY KEY,
region VARCHAR(10),
sale_date DATE,
amount INTEGER
);
INSERT INTO sales (id, region, sale_date, amount) VALUES
(1, 'East', '2025-01-01', 100),
(2, 'East', '2025-01-02', 50),
(3, 'East', '2025-01-02', 70),
(4, 'East', '2025-01-03', 30),
(5, 'West', '2025-01-01', 200),
(6, 'West', '2025-01-02', 20);Surprise 1: ties in a running total
A running total is the usual reason people write SUM(...) OVER (ORDER BY ...). Compare the default frame with an explicit ROWS frame:
SELECT
id,
region,
sale_date,
amount,
SUM(amount) OVER (
PARTITION BY region
ORDER BY sale_date
) AS default_total,
SUM(amount) OVER (
PARTITION BY region
ORDER BY sale_date, id
ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
) AS rows_total
FROM sales
ORDER BY region, sale_date, id;The result for the East region:
| id | sale_date | amount | default_total | rows_total |
|---|---|---|---|---|
| 1 | 2025-01-01 | 100 | 100 | 100 |
| 2 | 2025-01-02 | 50 | 220 | 150 |
| 3 | 2025-01-02 | 70 | 220 | 220 |
| 4 | 2025-01-03 | 30 | 250 | 250 |
Row 2 shows 220 under the default frame, even though only 150 of sales happened up to and including that row. The frame is RANGE, so the current row's peers count as "current". Row 3 has the same sale_date, so its 70 is included in row 2's total. Both tied rows show 220.
The default is not wrong. It treats all sales on the same date as one point in time. It becomes a bug when you read the output as "the balance after this specific row", for example in a ledger or a running inventory count.
The ROWS version counts physical rows. It also adds id to the ORDER BY, so the order among tied rows is fixed. Without that tiebreaker, ROWS still gives an answer, but the database may order rows 2 and 3 either way, and the totals can change between runs.
Surprise 2: LAST_VALUE returns the current row
FIRST_VALUE and LAST_VALUE look at the frame, not the partition. FIRST_VALUE happens to work under the default frame, because the frame always starts at the partition's first row. LAST_VALUE does not, because the frame ends at the current row.
SELECT
id,
region,
amount,
LAST_VALUE(amount) OVER (
PARTITION BY region
ORDER BY sale_date, id
) AS last_default,
LAST_VALUE(amount) OVER (
PARTITION BY region
ORDER BY sale_date, id
ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING
) AS last_full
FROM sales
ORDER BY region, sale_date, id;For the East region:
| id | amount | last_default | last_full |
|---|---|---|---|
| 1 | 100 | 100 | 30 |
| 2 | 50 | 50 | 30 |
| 3 | 70 | 70 | 30 |
| 4 | 30 | 30 | 30 |
The last_default column just repeats amount. Because id makes every row unique, each row has no peers. The last row of the frame is the current row itself.
If you order by sale_date alone, a peer group is part of the frame. Then LAST_VALUE returns the value from the last peer in that group, and which peer comes last is not defined. This is a worse failure than the one above, because the output looks plausible and can differ between runs.
The fix is to state the frame, as in the last_full column. The same applies to NTH_VALUE. With the default frame, NTH_VALUE(amount, 2) returns NULL on the first row, because the frame holds only one row there.
Surprise 3: adding ORDER BY changes an aggregate's meaning
Consider two windows that differ only by one clause:
SELECT
id,
region,
amount,
SUM(amount) OVER (PARTITION BY region) AS region_total,
SUM(amount) OVER (PARTITION BY region ORDER BY sale_date) AS cumulative
FROM sales
ORDER BY region, sale_date, id;The first column gives 250 on every East row and 220 on every West row. The second column grows as the dates advance. You never changed the function. Adding ORDER BY to the window switched the default frame from the whole partition to a growing one.
This causes trouble when someone adds ORDER BY to a window only to make output deterministic, or because a ranking function in the same query needs it. A percentage-of-total column such as amount * 100.0 / SUM(amount) OVER (PARTITION BY region ORDER BY id) then silently divides by a running total instead of the region total. If you want a whole-partition aggregate, give it its own window without ORDER BY.
Functions that ignore the frame
Frames only matter for functions that read a range of rows. These functions ignore any frame you write, and they use the whole partition in the window's order:
ROW_NUMBER,RANK,DENSE_RANKNTILE,PERCENT_RANK,CUME_DISTLAGandLEAD
You do not need to worry about the default frame for LAG or LEAD. It affects the aggregates (SUM, AVG, COUNT, MIN, MAX) and FIRST_VALUE, LAST_VALUE and NTH_VALUE.
When RANGE is what you want
The peer behaviour is useful in some cases. If you want "all sales up to and including this date", where every row on the same date shows the same figure, the default is correct. You can also write it explicitly, so a reader knows it is deliberate:
SELECT
sale_date,
SUM(amount) OVER (
ORDER BY sale_date
RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
) AS total_through_date
FROM sales;Choose by asking what a tied row should mean. If peers are one logical event, use RANGE. If each row is its own event and order among ties is meaningful or can be made so, use ROWS with a unique ORDER BY.
Performance and dialect notes
ROWSis often cheaper thanRANGE, because the engine does not have to find peer boundaries. In SQL Server, aRANGEframe can spill to disk for large partitions, whileROWSnormally does not. WritingROWS UNBOUNDED PRECEDINGexplicitly is a common tuning step there.ROWS UNBOUNDED PRECEDINGis shorthand forROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW. Both are accepted in the major databases.- Some databases restrict
RANGEwith numeric offsets (such asRANGE BETWEEN 5 PRECEDING AND CURRENT ROW).UNBOUNDED PRECEDINGandCURRENT ROWare supported everywhere the default needs them, so the default itself is portable.
A habit that avoids all of this
- Decide whether the function needs a frame at all (see the list above).
- If it does, write the frame, even when it matches the default.
- Use
ROWSunless you specifically want peers grouped together. - When using
ROWSwithORDER BY, add a unique column, such as the primary key, as the last sort key. - For
LAST_VALUEandNTH_VALUE, always check that the frame extends as far as you need.
Exercise
A query uses SUM(amount) OVER (PARTITION BY region ORDER BY sale_date) on the sales table above. For East, row id 2 (amount 50) and row id 3 (amount 70) share a date. The report shows 220 on both rows, but an auditor expects 150 on row 2 and 220 on row 3. What is the cause, and how do you fix it?
Show answer
The default frame with ORDER BY is RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW, which includes all peers of the current row. Rows 2 and 3 are peers because they have the same saledate, so both see 100 + 50 + 70 = 220. Change the window to ORDER BY saledate, id with ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW. Then each row sees only itself and the rows before it, giving 150 for row 2 and 220 for row 3.
Exercise
You write LASTVALUE(amount) OVER (PARTITION BY region ORDER BY saledate, id) to get each region's final sale amount, but every row just returns its own amount. Why, and what is the minimal change that returns the region's last amount on every row?
Show answer
The default frame ends at the current row, so the last row of the frame is the current row. Add an explicit frame that reaches the end of the partition: ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING (or ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING). Every row then sees the whole partition, and LAST_VALUE returns the final row's amount, such as 30 for East.
Exercise
Which of these functions are affected by the default frame: ROWNUMBER, LAG, SUM, FIRSTVALUE, LAST_VALUE?
Show answer
SUM, FIRSTVALUE and LASTVALUE read the frame. SUM changes meaning (running instead of whole-partition) when ORDER BY is present, and LASTVALUE returns the current row or last peer. FIRSTVALUE gives the partition's first row either way, because the default frame always starts there. ROW_NUMBER and LAG ignore frames.