Default Frame Trap with ORDER BY (RANGE UNBOUNDED PRECEDING AND CURRENT ROW)
Understand the default cumulative frame when ORDER BY is present.
In plain words
With ORDER BY and no frame, rows that tie are added together, so they all get the same running total.
Think of it like…
A race where runners who cross the line at the same instant share one finishing time: if you ask "how many have finished by now?" at either of them, the answer counts both.
Where you’ve seen it
A support dashboard plots cumulative tickets: COUNT(*) OVER (ORDER BY created_at). A bulk import created 40 tickets with the same timestamp, so the line jumps from 1,210 straight to 1,250 with 40 identical points instead of climbing one at a time. Switching to ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW with ORDER BY created_at, id makes each ticket add exactly one.
Words you’ll hear
- Default frame
- RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW, used when ORDER BY has no frame.
- Peers
- Rows with equal ORDER BY values; RANGE frames include all of them together.
- ROWS frame
- Counts physical rows, so each row's frame ends exactly at that row.
- No ORDER BY
- Without ORDER BY the frame is the whole partition.
Visual diagram
Theory & example queries
Where the default bites
The SQL standard default, followed by PostgreSQL, MySQL, SQL Server, Oracle and most other engines, ends the frame at the current row's last peer. With unique ORDER BY values, RANGE and ROWS give identical results, which is why the trap goes unnoticed in tests with tidy data. Ties appear in real data all the time: several orders on the same date, events in the same second, imports with one timestamp.
Three symptoms: running SUM or COUNT repeats the same value on tied rows and jumps by their combined amount; running AVG averages the tied rows together; and LAST_VALUE(x) OVER (ORDER BY t) returns the current row's value (or its last peer's), not the partition's final value, because the frame stops at the current row. FIRST_VALUE isn't affected, because the frame always starts at the beginning.
SELECT DAY, amount, SUM(amount) OVER ( ORDER BY DAY ) AS default_frame, SUM(amount) OVER ( ORDER BY DAY, id ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW ) AS rows_frameFROM sales;PostgreSQL is the lesson baseline unless noted otherwise.
Calculate the selected expressions and return the requested output columns.
Expected output4 sample rows
| day | amount | default_frame | rows_frame |
|---|---|---|---|
| 2026-11-01 | 100 | 300 | 100 |
| 2026-11-01 | 200 | 300 | 300 |
| 2026-11-02 | 150 | 450 | 450 |
| 2026-11-03 | 75 | 525 | 525 |
Note: Illustrative tied-day rows; RANGE by the default frame includes both rows of 2026-11-01, while ROWS stops at the current row.
On tied days the two columns differ.
Key takeaway: Ties + default frame = shared running totals and wrong LAST_VALUE.
When RANGE is right, and the habit to adopt
Sometimes peers sharing a value is exactly what you want: a cumulative total by date where every order on a day should show the end-of-day figure. Even then, it's clearer to aggregate to one row per day first and run the cumulative sum over days, so nobody wonders whether the behaviour is intentional.
Adopt one habit: whenever a window function has ORDER BY and is an aggregate or a value function, write the frame explicitly and make ORDER BY unique with a tie-breaker such as id. It documents intent and makes results deterministic. In SQL Server it can also be faster, since the default RANGE frame uses an on-disk worktable while ROWS can use an in-memory spool.
| id | customer_id | status | amount | coupon_code |
|---|---|---|---|---|
| 101 | 1 | paid | 249 | NULL |
| 102 | 2 | paid | 120 | WELCOME10 |
| 103 | 1 | shipped | 89.5 | NULL |
SELECT customer_id, order_id, LAST_VALUE(order_id) OVER ( PARTITION BY customer_id ORDER BY created_at, order_id ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS latest_order_idFROM orders;PostgreSQL is the lesson baseline unless noted otherwise.
Calculate the selected expressions and return the requested output columns.
Expected output4 sample rows
| customer_id | order_id | latest_order_id |
|---|---|---|
| 42 | 501 | 530 |
| 42 | 518 | 530 |
| 42 | 530 | 530 |
| 77 | 512 | 524 |
Note: Illustrative orders; the full-partition frame gives every row the latest ID within its customer partition.
Key takeaway: Write the frame and a tie-breaker every time; aggregate first if you want per-day totals.
How it works, step by step
Follow one practical SQL workflow from start to finish. Select any step or play through the flow.
Spot ORDER BY without a frame
Look for SUM, COUNT, AVG or LAST_VALUE with OVER (… ORDER BY …) and no ROWS or RANGE clause.
SUM(amount) OVER ( ORDER BY DAY)When to use it & trade-offs
What it is
When a window has ORDER BY but no frame clause, SQL uses the default frame RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW. RANGE treats rows with the same ORDER BY value as peers and includes all of them, so tied rows get the same running total, and LAST_VALUE returns the last peer instead of the partition's last row. Writing ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW explicitly gives the row-by-row behaviour most people expect.
Why it matters: The trap is silent: the query runs, the numbers look plausible, and the bug only shows on rows with tied timestamps or dates. It surfaces as running totals that jump by two orders at once, statements where consecutive lines show the same balance, and LAST_VALUE columns that equal the current row. It's a favourite interview question because it separates people who know the defaults from people who guess.
When to reach for it
- Explicit ROWS frames for every running total
- UNBOUNDED FOLLOWING for LAST_VALUE over a partition
- Code review checks on window functions
When to avoid or be careful
- Relying on the default frame in shared or production SQL
- ORDER BY on non-unique columns without a tie-breaker
- LAST_VALUE with ORDER BY and no frame
Default RANGE vs explicit ROWS vs aggregate first
Strengths: No extra syntax.
Watch out: Silent surprises on ties; wrong LAST_VALUE; slower in SQL Server.
Strengths: One row at a time; deterministic; clear intent.
Watch out: Longer to write.
Strengths: No peers left, so RANGE and ROWS agree.
Watch out: Loses the individual rows.
Interview signals & numbers to know
When this topic comes up
- “Why do two rows have the same running total?”
- “LAST_VALUE returns the current row”
- “What's the default window frame?”
- Running totals with duplicate dates
- Deterministic window results
Benchmarks & rules of thumb
- RANGE … CURRENT ROW
- default frame with ORDER BY
- Whole partition
- default frame without ORDER BY
- 0
- difference between RANGE and ROWS when ORDER BY is unique
Common mistakes & follow-ups
Common interview mistakes
- Testing a running balance on data with unique timestamps, then shipping it to data where many rows share a date.
- Using LAST_VALUE(price) OVER (PARTITION BY product ORDER BY changed_at) to get the latest price and getting each row's own price.
- Fixing the frame but leaving ORDER BY non-unique, so the order of tied rows still changes between runs.
Likely interviewer follow-ups
- What frame does SUM() OVER (ORDER BY day) use, and when does it matter?
- Why does LAST_VALUE often return the current row's value?
- When would you deliberately want RANGE behaviour?
Knowledge check
Put it into practice
Practice related interview Q&A
Apply Default Frame Trap with ORDER BY (RANGE UNBOUNDED PRECEDING AND CURRENT ROW) with hands-on SQL practice and interview Q&A.