Sub topic 13
Reusing Definitions with the `WINDOW` Clause
Assume you know SELECT, GROUP BY, aggregate functions, CTEs, and basic window functions. This chapter shows how to name a window definition and reuse it across several functions.
Why name a window?
A window definition describes the rows that a window function can use. It can include:
PARTITION BY, which separates rows into groupsORDER BY, which determines row order inside each group- A frame clause, such as
ROWS BETWEEN
Without a name, you repeat the definition:
SUM(amount) OVER (
PARTITION BY region
ORDER BY sale_date, id
ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
)If several functions use the same definition, repetition makes queries longer and increases the chance of inconsistent changes. The WINDOW clause gives that definition a name.
Defining and reusing a window
Place the WINDOW clause after FROM and before the query's final ORDER BY.
WITH sales(id, region, sale_date, amount) AS (
VALUES
(1, 'North', '2026-01-03', 100),
(2, 'North', '2026-01-05', 150),
(3, 'South', '2026-01-04', 200),
(4, 'South', '2026-01-06', 120)
)
SELECT
region,
sale_date,
amount,
SUM(amount) OVER running_sales AS running_total,
AVG(amount) OVER running_sales AS running_average
FROM sales
WINDOW running_sales AS (
PARTITION BY region
ORDER BY sale_date, id
ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
)
ORDER BY region, sale_date, id;The name running_sales refers to the definition inside the WINDOW clause. Both SUM and AVG use the same partition, order, and frame.
For the second North row, the running total is 250, and the running average is 125. The South rows have their own separate calculations because PARTITION BY region starts a new window for each region.
Reusing and extending definitions
A named window can also provide a shared base definition. Some database systems allow another window to inherit from it:
SELECT
region,
sale_date,
amount,
SUM(amount) OVER ordered_rows AS running_total,
AVG(amount) OVER (
ordered_rows
ROWS BETWEEN 2 PRECEDING AND CURRENT ROW
) AS moving_average
FROM sales
WINDOW ordered_rows AS (
PARTITION BY region
ORDER BY sale_date, id
);Here, both functions share the partition and order. The second function supplies a different frame.
Check your database documentation before using window inheritance. Basic named windows are supported by PostgreSQL, MySQL 8+, and SQLite, but SQL Server does not support the WINDOW clause.
Common errors
- Put
WINDOWafterFROM,JOIN, andWHERE, but before the finalORDER BY. - Do not confuse the final query
ORDER BYwith the window'sORDER BY. The first controls displayed rows; the second controls calculations. - Add a unique tie-breaker such as
idwhen row order must be deterministic. - Do not define conflicting clauses when extending a named window. For example, overriding an inherited
ORDER BYmay be rejected.
Exercise
Why might a query use a named window instead of repeating the same PARTITION BY and ORDER BY clauses?
Show answer
It centralizes the definition, reduces repetition, and helps multiple window functions use exactly the same rows and ordering.