Databases & SQL · reviewed in July 2026
Window function
A SQL window function computes a value across a set of related rows — its 'window' — without collapsing them into a single row the way GROUP BY does. It lets you calculate, for example, a ranking or a moving average while keeping every original row visible in the result.
Frequently asked questions
How is it different from GROUP BY?
GROUP BY reduces several rows into a single row per group; a window function computes the aggregate but keeps all original rows, adding the result as just another column.
How do you define a row's window?
With the OVER clause, which can include PARTITION BY (to split into groups) and ORDER BY (to define an order within each group, key for functions like ranking or moving averages).
What are the most common window functions?
ROW_NUMBER (numbers the rows), RANK and DENSE_RANK (assign a ranking), and LAG/LEAD (access the previous or next row's value within the same window).