The WINDOW clause is part of the SELECT statement in the SQL standard. Positioned after GROUP BY and HAVING (if present) and before ORDER BY, it lets you declare reusable, named window definitions. Each name captures the partitioning, ordering, and frame boundaries that window (analytic) functions require. By centralizing the definition, you avoid repeating identical OVER() clauses, keep queries readable, and guarantee that multiple aggregates operate over the exact same rows.A window specification can inherit another named window and extend or override parts of it. This composability supports complex analytics while minimizing duplication.Caveats:- WINDOW is not available in every database even if that database supports window functions.- Names must be unique within the statement.- A named window cannot reference itself, and cyclical inheritance is prohibited.- A named window cannot include another WINDOW clause.Behavior is purely logical: WINDOW only defines scopes; it does not change the result set until paired with analytic functions.
window_name
(identifier) - Label used later inside OVER(window_name)window_definition
(clause) - PARTITION BY, ORDER BY, and frame specification that describe the windowOVER, PARTITION BY, ORDER BY, ROWS BETWEEN, RANGE BETWEEN, WINDOW FUNCTIONS
SQL:2003
PostgreSQL, MySQL 8.0, MariaDB, DuckDB, Trino, BigQuery, and most engines that closely follow the SQL standard. SQL Server and Oracle currently lack support.
OVER() attaches a window to an individual analytic function. WINDOW defines the window once and lets many OVER(window_name) references reuse it, keeping the query concise.
Yes. You can list multiple name-definition pairs separated by commas within a single WINDOW clause block.
Performance is usually unchanged. It only removes duplication in the query text. The optimizer still evaluates each analytic function, often sharing computation when possible.