CAB206 · Unit 3: OLAP and SQL for BI
Window functions and time intelligence in SQL
10 practice questions
Key points
- Window functions keep all rows; GROUP BY collapses
- PARTITION BY restarts per group; ORDER BY orders within it
- RANK skips after ties, DENSE_RANK does not, ROW_NUMBER is unique
- LAG gives the previous row for period change
- Default frame with ORDER BY is RANGE, so ties share a running total
Window functions compute a value over a set of related rows without collapsing them, unlike GROUP BY which returns one row per group.
function() OVER (PARTITION BY ... ORDER BY ... frame)
PARTITION BYrestarts the calculation for each group (for example, per region).ORDER BYdefines the order inside each partition.- A frame (
ROWS BETWEEN ...) restricts which rows around the current one are used.
Common functions
- Running total:
SUM(x) OVER (ORDER BY month) - Moving average:
AVG(x) OVER (ORDER BY month ROWS BETWEEN 2 PRECEDING AND CURRENT ROW) - Previous row:
LAG(x); next row:LEAD(x). Period change =x - LAG(x). - Percent of total:
100.0 * x / SUM(x) OVER () - Ranking:
ROW_NUMBER()gives unique numbers;RANK()gives ties the same rank and then skips numbers;DENSE_RANK()gives ties the same rank without skipping.
Default frame trap. When ORDER BY is present and no frame is given, the default frame is RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW, so rows with equal sort values (peers) are included together. A running total over tied values therefore repeats the same figure for the peers. Use ROWS for a strict row-by-row running total.
Order of evaluation. Window functions run after WHERE, GROUP BY and HAVING. A WHERE filter therefore shrinks the window: 100.0*amt/SUM(amt) OVER () with WHERE month = 4 returns 100%, because only one row is left. Compute the percentage first in a subquery or CTE, then filter.
Time intelligence. Typical measures are month-over-month (MoM) and year-over-year (YoY) change, year-to-date, and moving averages. YoY = (this period - same period last year) / same period last year. A CTE (WITH name AS (...)) names an intermediate result and makes such queries readable.
Try two questions
Which function returns the value from the previous row in the window order, useful for period-over-period change?
- RANK
- LAG
- LEAD
- NTILE
Show the answer
B. LAG reads the previous row; LEAD reads the next row.
Monthly sales are 100, 120, 90, 150, 150. The query SELECT 100.0*amt/SUM(amt) OVER () FROM m WHERE mon = 4 is run to get April's share of the total. What does it return?
- 24.6, because the window always sums the whole table regardless of WHERE
- 100.0, because WHERE removes the other months before the window is computed
- 150.0, because the window simply returns the row's own amount unchanged
- NULL, because OVER () cannot be used without an ORDER BY clause
Show the answer
B. Window functions are evaluated after WHERE, so only April remains and the window total is 150, giving 100.0. Compute the share in a subquery first, then filter, to get 24.6.