Sub topic 9
Controlling the frame: ROWS, RANGE and GROUPS
This chapter assumes you can write an OVER (PARTITION BY ... ORDER BY ...) clause and know what an aggregate such as SUM does. It covers the frame clause, which decides which rows inside a partition a window function actually reads for the current row. The examples use a sales table with the columns id, region, sale_date, and amount.
What a frame is
A partition is the group of rows that share the same PARTITION BY values. A frame is a sliding sub-range of that partition, defined relative to the current row. Each row gets its own frame, and the aggregate is computed over that frame only.
The general shape is:
function() OVER (
PARTITION BY ...
ORDER BY ...
frame_unit frame_start -- or: frame_unit BETWEEN frame_start AND frame_end
)The frame has two parts: a unit (ROWS, RANGE, or GROUPS) and two boundaries.
Frames only matter for functions that read a range of rows, such as SUM, AVG, COUNT, MIN, MAX, FIRST_VALUE, LAST_VALUE, and NTH_VALUE. Ranking functions (ROW_NUMBER, RANK, DENSE_RANK) and LAG/LEAD ignore the frame. They work from the partition and its ordering.
Frame boundaries
Each boundary is one of these five forms:
| Boundary | Meaning |
|---|---|
| `UNBOUNDED PRECEDING` | The first row of the partition |
| `N PRECEDING` | N units before the current row |
| `CURRENT ROW` | The current row (and, for `RANGE` and `GROUPS`, its peers) |
| `N FOLLOWING` | N units after the current row |
| `UNBOUNDED FOLLOWING` | The last row of the partition |
The start boundary cannot come after the end boundary. For example, BETWEEN CURRENT ROW AND 2 PRECEDING is an error. If you write only one boundary, such as ROWS 2 PRECEDING, the end defaults to CURRENT ROW.
ROWS: counting physical rows
ROWS counts rows by position in the ordered partition. 2 PRECEDING means "the two rows above this one", whatever their values are.
This is the most predictable unit, and it is the one to reach for first.
SELECT
region,
sale_date,
amount,
SUM(amount) OVER (
PARTITION BY region
ORDER BY sale_date, id
ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
) AS running_total,
AVG(amount) OVER (
PARTITION BY region
ORDER BY sale_date, id
ROWS BETWEEN 2 PRECEDING AND CURRENT ROW
) AS moving_avg_3
FROM sales;The running_total column adds every row from the start of the region up to the current row. The moving_avg_3 column averages the current row and the two before it. On the first row of a region there are no earlier rows, so the frame holds one row and the average is that row's own amount. The frame is shorter at the edges of the partition. It is not padded.
The id in the ORDER BY is a tie-breaker. If two sales in a region share a sale_date, the database is free to put them in either order, and a ROWS frame would then give different totals from one run to the next. Adding a unique column makes the order deterministic.
RANGE: grouping by value
RANGE defines the frame by the value of the ORDER BY expression, not by position. Rows that tie on the ordering value are called peers, and RANGE always treats peers as a unit. With CURRENT ROW, the frame includes the current row and all of its peers.
Suppose a region has these sales, ordered by sale_date:
| sale_date | amount |
|---|---|
| 2026-01-01 | 10 |
| 2026-01-02 | 20 |
| 2026-01-02 | 30 |
| 2026-01-03 | 40 |
This query compares the two units side by side:
SELECT
sale_date,
amount,
SUM(amount) OVER (ORDER BY sale_date
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
FROM sales;For the first row on 2026-01-02, rows_total is 30 (10 + 20) and range_total is 60 (10 + 20 + 30), because the second 2026-01-02 row is a peer and falls inside the frame. For the second row on 2026-01-02, both columns give 60. Since the order of tied rows is arbitrary, rows_total for the two tied rows could appear as 30 and 60 or as 40 and 60. range_total is 60 for both, every time.
RANGE with offsets
RANGE can also take a numeric offset, such as RANGE BETWEEN 6 PRECEDING AND CURRENT ROW. This means "rows whose ordering value is within 6 of the current row's value". It requires exactly one ORDER BY expression, and that expression must be numeric or a date type in most databases.
Offset support for dates varies. PostgreSQL 11 and later accept RANGE BETWEEN INTERVAL '6 days' PRECEDING AND CURRENT ROW on a date or timestamp column. SQL Server does not accept offset RANGE with values other than UNBOUNDED and CURRENT ROW. Check your database's documentation before relying on it.
GROUPS: counting peer groups
GROUPS counts peer groups, meaning sets of rows with the same ORDER BY value. 1 PRECEDING means "the previous distinct value, with all of its rows".
SELECT
sale_date,
amount,
SUM(amount) OVER (
ORDER BY sale_date
GROUPS BETWEEN 1 PRECEDING AND CURRENT ROW
) AS two_day_total
FROM sales;With the sample data above, the rows on 2026-01-02 get a two_day_total of 60 (all of 01-01 plus both rows of 01-02). The row on 2026-01-03 gets 90 (both rows of 01-02 plus itself). GROUPS is useful when you want "the last N distinct days" without caring how many rows each day holds.
GROUPS is part of the SQL standard but has narrower support. PostgreSQL 11+ and SQLite 3.28+ support it. MySQL and SQL Server do not.
How the three units differ
| Unit | Counts | Ties | Offsets | Support |
|---|---|---|---|---|
| `ROWS` | Physical rows | Ignored, so order must be made deterministic | Any non-negative integer | Widely supported |
| `RANGE` | Values of the ORDER BY expression | Peers are always included together | Numeric or date distance, with limits | `UNBOUNDED`/`CURRENT ROW` everywhere, offsets vary |
| `GROUPS` | Peer groups | Peers are always included together | Any non-negative integer | PostgreSQL, SQLite |
Frame exclusion and defaults
When you write ORDER BY in the window and no frame clause, the default frame is RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW. Without ORDER BY, the frame is the whole partition. Because the default uses RANGE, a running total over a non-unique ordering gives tied rows the same total. Writing ROWS explicitly changes that behavior, so state the unit whenever it matters.
The default frame also causes a well-known surprise with LAST_VALUE, because the frame ends at the current row. To read the last value in the partition, widen the frame:
SELECT
region,
sale_date,
amount,
LAST_VALUE(amount) OVER (
PARTITION BY region
ORDER BY sale_date, id
ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING
) AS last_amount_in_region
FROM sales;A later chapter treats the default frame and the value functions in more detail.
Common mistakes
- Using
RANGEfor a running total and being surprised that tied rows share a total. ChooseROWSwith a unique tie-breaker if you want one total per row. - Omitting a tie-breaker from the
ORDER BYin aROWSframe, which makes results change between runs. - Writing
ROWS BETWEEN 6 PRECEDING AND CURRENT ROWand calling it a "7-day average" when some days are missing. The frame is 7 rows, not 7 days. - Putting a frame on
ROW_NUMBER,RANK,LAG, orLEAD. Some databases reject it and others ignore it. - Using a
RANGEoffset with severalORDER BYcolumns, or with a text column. This is an error.
Exercise
A table has rows ordered by day with amounts 5, 10, 10, 20 on days 1, 2, 2, 3. For the row on day 3, what does SUM(amount) OVER (ORDER BY day GROUPS BETWEEN 1 PRECEDING AND CURRENT ROW) return, and what would ROWS BETWEEN 1 PRECEDING AND CURRENT ROW return?
Show answer
The GROUPS frame covers the previous peer group (both day-2 rows, 10 + 10) plus the current row (20), so it returns 40. The ROWS frame covers only the one physical row above (a day-2 row with amount 10) plus the current row, so it returns 30.
Exercise
You want a running total per region that gives each sale its own distinct value, even when two sales share a date. Which frame unit and which ORDER BY would you use?
Show answer
Use ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW with ORDER BY sale_date, id. ROWS counts physical rows, and the unique id makes the order of tied dates deterministic. RANGE would give tied rows the same total.