Sub topic 11
The Default Frame and Why It Surprises People
This chapter assumes you know SELECT, GROUP BY, aggregate functions, subqueries, and common table expressions (CTEs). You also need a database that supports window functions, such as PostgreSQL, MySQL 8+, SQL Server, SQLite 3.25+, or Oracle.
A window has two related parts:
- The partition is the group of rows being considered.
- The frame is the smaller set of rows used for the current row.
For example, a window can contain all sales in one region, while its frame contains only the sales from the beginning of that region through the current sale.
What happens when you omit the frame
Consider this query:
SELECT
region,
sale_date,
amount,
SUM(amount) OVER (
PARTITION BY region
ORDER BY sale_date
) AS running_total
FROM sales;The ORDER BY tells the database to process each region in date order. Because no frame is written, the database applies a default frame. In many databases, that frame is conceptually:
RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROWThis means “from the start of the partition through the current row and its peers.” Peers are rows with the same values in the window ORDER BY.
The default is often what you want for a simple running total. It becomes risky when dates or sort values repeat, or when you use value functions such as LAST_VALUE.
| Frame | Meaning |
|---|---|
| `ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW` | Physical rows from the first row through the current row |
| `RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW` | Values from the start through the current sort value, including peers |
| `ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING` | Every row in the partition |
The `LAST_VALUE` surprise
Suppose a region has several sales. You might expect this query to return the final sale amount for every row:
SELECT
region,
sale_date,
amount,
LAST_VALUE(amount) OVER (
PARTITION BY region
ORDER BY sale_date
) AS last_amount
FROM sales;Usually, it does not. With the default frame, the frame ends at the current row. Therefore, LAST_VALUE returns the last value inside that frame, which is normally the current row's amount.
To get the last value in the entire region, extend the frame explicitly:
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;The id makes the order deterministic when two sales have the same date.
`ROWS` versus `RANGE`
Suppose two sales occur on the same date. With RANGE, both rows are peers. A running total may show the same total for both rows because the frame includes all sales on that date.
With ROWS, the frame advances one physical row at a time. Use it when each row must be treated separately:
SELECT
region,
sale_date,
id,
amount,
SUM(amount) OVER (
PARTITION BY region
ORDER BY sale_date, id
ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
) AS running_total
FROM sales;The best practice is to write ROWS explicitly for running totals and moving calculations. Use RANGE when equal sort values should be handled as one group.
Exercise
Why can LAST_VALUE(amount) return the current amount instead of the final amount in the region?
Show answer
The default frame usually ends at the current row. Use ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING to include the entire partition.