Window Frame Clauses (ROWS vs RANGE vs GROUPS)
Control which peer rows participate using ROWS, RANGE, or GROUPS frames.
In plain words
The frame is the slice of rows a window function looks at for each row. ROWS counts rows, RANGE measures values, GROUPS counts sets of ties.
Think of it like…
Taking an average of "recent" exam scores: ROWS means "the last 3 exams I took", RANGE means "every exam in the last 30 days", and GROUPS means "the last 3 exam dates, counting every exam taken on each date".
Where you’ve seen it
Daily revenue has two rows for some days (two regions). SUM(rev) OVER (ORDER BY day ROWS 6 PRECEDING) covers 7 rows, which may be only 4 days; RANGE BETWEEN INTERVAL '6 days' PRECEDING AND CURRENT ROW covers exactly 7 calendar days, whatever the row count.
Words you’ll hear
- Frame
- The set of rows, relative to the current row, that the aggregate uses.
- Peers
- Rows with the same ORDER BY value as the current row.
- Default frame
- RANGE UNBOUNDED PRECEDING to CURRENT ROW with ORDER BY; the whole partition without it.
- EXCLUDE
- PostgreSQL 11+ option to drop the current row, its group or its ties from the frame.
Visual diagram
Theory & example queries
ROWS, RANGE and GROUPS
A frame is written as {ROWS | RANGE | GROUPS} BETWEEN start AND end, where start and end are UNBOUNDED PRECEDING, n PRECEDING, CURRENT ROW, n FOLLOWING or UNBOUNDED FOLLOWING. ROWS counts rows, so it's precise but blind to dates and ties.
RANGE measures distance in ORDER BY values: RANGE BETWEEN INTERVAL '7 days' PRECEDING AND CURRENT ROW needs a single date or number ORDER BY column (PostgreSQL 11+, MySQL 8; SQL Server allows only UNBOUNDED and CURRENT ROW with RANGE). In RANGE mode, CURRENT ROW means the current row and all its peers. GROUPS counts distinct ORDER BY values, so GROUPS 1 PRECEDING means the previous set of tied rows plus the current set.
| day | amount |
|---|---|
| 2026-11-15 | 100 |
| 2026-11-17 | 200 |
| 2026-11-21 | 50 |
| 2026-11-22 | 75 |
SELECT DAY, SUM(amount) OVER ( ORDER BY DAY RANGE BETWEEN INTERVAL '6 days' PRECEDING AND CURRENT ROW ) AS rolling_7_day_revenueFROM daily_revenueORDER BY DAY;PostgreSQL is the lesson baseline unless noted otherwise.
Calculate the selected expressions and return the requested output columns.
Expected output4 sample rows
| day | rolling_7_day_revenue |
|---|---|
| 2026-11-15 | 100 |
| 2026-11-17 | 300 |
| 2026-11-21 | 350 |
| 2026-11-22 | 325 |
Note: Sample input uses amount values 100, 200, 50 and 75. RANGE measures six calendar days, so November 15 is included on November 21 but excluded on November 22; the source amount column is not part of the SELECT list.
Key takeaway: ROWS = row count, RANGE = value distance (and peers), GROUPS = tie groups.
The default frame and the LAST_VALUE trap
When OVER has ORDER BY but no frame, the frame is RANGE from the start of the partition to the current row, including peers. That's why a running SUM shows the same total on tied rows, and why LAST_VALUE(x) OVER (ORDER BY ts) returns the current row's value: the frame ends at the current row.
For the real last value in the partition, extend the frame: LAST_VALUE(x) OVER (PARTITION BY g ORDER BY ts ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING). Without ORDER BY, the frame is the whole partition, which is why SUM(x) OVER (PARTITION BY g) gives a group total.
| trade_date | symbol | price | ts |
|---|---|---|---|
| 2026-11-21 | AAPL | 198.2 | 09:15:00 |
| 2026-11-21 | AAPL | 198.8 | 15:30:00 |
| 2026-11-21 | MSFT | 410.1 | 09:15:00 |
| 2026-11-21 | MSFT | 410.45 | 15:30:00 |
SELECT trade_date, symbol, price, LAST_VALUE(price) OVER ( PARTITION BY trade_date, symbol ORDER BY ts ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS close_priceFROM tradesORDER BY trade_date, ts;PostgreSQL is the lesson baseline unless noted otherwise.
Calculate the selected expressions and return the requested output columns.
Expected output4 sample rows
| trade_date | symbol | price | close_price |
|---|---|---|---|
| 2026-11-21 | AAPL | 198.2 | 198.8 |
| 2026-11-21 | AAPL | 198.8 | 198.8 |
| 2026-11-21 | MSFT | 410.1 | 410.45 |
| 2026-11-21 | MSFT | 410.45 | 410.45 |
Note: The query returns each trade beside the final price for that symbol and date.
Key takeaway: Write the frame explicitly whenever ORDER BY is present.
How it works, step by step
Follow one practical SQL workflow from start to finish. Select any step or play through the flow.
Decide what "nearby" means
Choose between a number of rows, a span of values (days, amounts) or a number of tie groups.
last 7 rows vs last 7 daysWhen to use it & trade-offs
What it is
A window frame decides which rows around the current one an aggregate window function uses. ROWS counts physical rows ("the 2 rows before"). RANGE uses values of the ORDER BY column ("rows within 7 days before", and all rows tied with the current one). GROUPS counts groups of tied rows. With ORDER BY and no explicit frame, the default is RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW.
Why it matters: The frame silently changes results: running totals that jump on tied rows, LAST_VALUE that returns the current row instead of the last, and rolling averages that cover the wrong dates. Advanced SQL interviews use frames to separate people who know window functions from people who have only copied them.
When to reach for it
- Rolling metrics over calendar periods
- Running totals where tie behaviour matters
- LAST_VALUE and NTH_VALUE over the whole partition
When to avoid or be careful
- Relying on the default frame with ORDER BY
- ROWS-based "7-day" windows on data with gaps
- LAST_VALUE without extending the frame
ROWS vs RANGE vs GROUPS
Strengths: Precise and deterministic with a tiebreaker; supported everywhere.
Watch out: Ignores dates and ties: 7 rows may not be 7 days.
Strengths: Follows actual values, so gaps in dates are handled.
Watch out: Needs one ORDER BY column for offsets; limited in SQL Server.
Strengths: Treats each tie group as one step.
Watch out: Not in MySQL or SQL Server; rarely needed.
Interview signals & numbers to know
When this topic comes up
- “Why does LAST_VALUE return the current row?”
- “ROWS vs RANGE”
- Running total repeats on tied rows
- "Last 7 days" window
- Window frame
Benchmarks & rules of thumb
- RANGE … CURRENT ROW
- the default frame with ORDER BY
- PostgreSQL 11
- GROUPS, RANGE offsets and EXCLUDE
- Whole partition
- the frame without ORDER BY
Common mistakes & follow-ups
Common interview mistakes
- Using LAST_VALUE with the default frame and getting the current row's value.
- Calling a ROWS 6 PRECEDING window "7 days" when some days are missing or repeated.
- Assuming SQL Server supports RANGE with numeric or interval offsets.
Likely interviewer follow-ups
- What's the default window frame when ORDER BY is present?
- How do ROWS and RANGE differ when there are tied values?
- How would you get the last value in each partition on every row?
Knowledge check
Put it into practice
Practice related interview Q&A
Apply Window Frame Clauses (ROWS vs RANGE vs GROUPS) with hands-on SQL practice and interview Q&A.