The OVER clause turns an ordinary aggregate, ranking, or analytic function into a window function. Instead of returning one summary row, the function is evaluated for every input row across a logical window defined by PARTITION BY, ORDER BY, and an optional frame (ROWS or RANGE). The clause lets you calculate running totals, moving averages, rankings, percentiles, and other advanced analytics while still keeping the original granular rows. If PARTITION BY is omitted, the entire result set is treated as a single partition. If ORDER BY is omitted, the window is unordered, so the frame defaults to all rows in the partition. Frame specifications such as ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW further narrow the set of rows used in each calculation. The OVER clause does not change the number of rows returned—one output row is produced for every input row—nor does it allow filtering; that must be done with WHERE or HAVING. Because window functions are evaluated after WHERE, GROUP BY, and HAVING but before ORDER BY, they cannot be referenced in those earlier clauses in most databases.
PARTITION BY
(expression list) - divides rows into independent windows.ORDER BY
(expression list) - defines ordering inside each partition.ROWS/RANGE frame_spec
(window frame) - sets start and end relative to current row.WINDOW, PARTITION BY, ORDER BY, ROWS BETWEEN, RANGE BETWEEN, aggregate functions, analytic functions, RANK, ROW_NUMBER
SQL:2003 standard
GROUP BY collapses rows into aggregates, returning one row per group. OVER keeps all original rows and adds a computed value for each.
No, but ORDER BY is required for calculations that depend on row order such as running totals, moving averages, or rankings.
Wrap the query using the window function in a subquery or common table expression, then apply the filter in an outer WHERE clause.
Usually window functions are faster because the database can compute them in a single pass without materializing joins, but performance depends on indexes, partition size, and order columns.