Sub topic 9
Controlling the Frame: `ROWS`, `RANGE` and `GROUPS`
This chapter assumes you know SELECT, GROUP BY, aggregate functions, CTEs, and basic window functions. A window frame is the subset of a window that a function uses for the current row. Choosing the frame changes the result of running totals, moving averages, and other aggregate calculations.
Why the frame matters
The ORDER BY inside OVER defines an order. The frame defines which ordered rows are included for each result.
Consider these sales:
| id | sale_date | amount |
|---|---|---|
| 1 | 2026-01-01 | 10 |
| 2 | 2026-01-01 | 20 |
| 3 | 2026-01-02 | 5 |
| 4 | 2026-01-03 | 7 |
| 5 | 2026-01-03 | 8 |
Rows with the same sale_date are peers. The three frame units behave differently:
ROWScounts physical rows.RANGEincludes rows with the sameORDER BYvalue as the current row.GROUPScounts peer groups rather than individual rows.
Comparing `ROWS`, `RANGE` and `GROUPS`
The following query calculates three cumulative totals:
WITH sales(id, sale_date, amount) AS (
VALUES
(1, '2026-01-01', 10),
(2, '2026-01-01', 20),
(3, '2026-01-02', 5),
(4, '2026-01-03', 7),
(5, '2026-01-03', 8)
)
SELECT
id,
sale_date,
amount,
SUM(amount) OVER (
ORDER BY sale_date, id
ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
) AS rows_total,
SUM(amount) OVER (
ORDER BY sale_date
RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
) AS range_total,
SUM(amount) OVER (
ORDER BY sale_date
GROUPS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
) AS groups_total
FROM sales
ORDER BY sale_date, id;The ROWS frame advances one physical row at a time. Because id makes the order unique, the totals are 10, 30, 35, 42, and 50.
The RANGE frame advances by ordering value. Both rows dated January 1 include both January 1 sales, so both show 30. Both January 3 rows show 50.
The GROUPS frame advances by peer group. With this unbounded cumulative frame, it produces the same totals as RANGE. Its advantage appears when you count a limited number of groups.
For example, this frame calculates a total for the current date and the previous two distinct dates:
SUM(amount) OVER (
ORDER BY sale_date
GROUPS BETWEEN 2 PRECEDING AND CURRENT ROW
)A ROWS BETWEEN 2 PRECEDING AND CURRENT ROW frame would instead use only the previous two physical rows. Those are different meanings when several rows share a date.
Default frames and deterministic results
When an ordered window omits its frame, many databases use a RANGE-like frame ending at CURRENT ROW. This can make duplicate ordering values behave as one group. State the frame explicitly when the result matters.
For a ROWS frame, add a unique tie-breaker such as id to the window ORDER BY. Without it, the database may choose an unspecified order among tied rows.
Exercise
You need a running total where every sale on the same day shows the same daily cumulative value. Which frame should you choose?
Show answer
Use RANGE or GROUPS ordered by sale_date, with a frame ending at CURRENT ROW. Use ROWS only when you want the total to change for each physical sale.