A SQL function that computes across a set of related rows, such as a running total or a rank, while keeping every row in the result.
A window function computes a value over a group of rows related to the current row, without collapsing them the way GROUP BY does. It is how SQL produces running totals, ranks, period-over-period changes and each row's share of its group in one query, with the detail still visible.
The function is followed by OVER, which names the window: SUM(revenue) OVER (PARTITION BY rep ORDER BY month). PARTITION BY sets the groups, ORDER BY sets the sequence within each, and an optional frame such as ROWS BETWEEN 2 PRECEDING AND CURRENT ROW limits the rows. Other common ones are ROW_NUMBER, RANK and LAG.
A rep with monthly revenue of $10,000, $12,000 and $8,000 gets a running total of $10,000, $22,000 and $30,000. LAG(revenue) on the third month returns $12,000, a change of −$4,000.
Leaving out ORDER BY, so the running total uses an arbitrary order. Forgetting that the default frame with ORDER BY groups tied dates together. Missing months, which make LAG compare non-adjacent periods. The full guide is SQL for data analysis.