Window Functions
Learn SQL window functions with practical examples and guided drills on dbSyntax.
Start with OVER / PARTITION BY / ORDER BYOVER / PARTITION BY / ORDER BY
IntermediateA window function computes a value across a set of related rows, without collapsing them the way GROUP BY does. Every input row stays in the...
ROW_NUMBER / RANK / DENSE_RANK
IntermediateThree ranking functions, one job: number rows inside a window in order. They differ only in how they handle ties. | Function | Ties | Gaps a...
LAG / LEAD
IntermediateLAG pulls the previous row's value into the current row. LEAD pulls the next row's value. Together they turn per-row comparisons ("change vs...
Moving Averages & Running Totals
IntermediateA running total is a windowed SUM that accumulates from the start of a series to the current row. A moving average is a windowed AVG over th...
QUALIFY
IntermediateEvery window-function lesson so far has hit the same wall: you compute rn, then you can't filter on it....