Start studying
Module 5 · SQL Module contents
Module 5  ·  Card 14 / 14advanced

Window Functions

A window function performs calculations across a set of table rows related to the current row, without collapsing the rows into a single summary.

Unlike GROUP BY, which reduces multiple rows into one, window functions retain the original row-level granularity of the dataset.

The calculation is defined using the OVER clause, which specifies how rows are partitioned and ordered for evaluation.

Common window functions include ROW_NUMBER for numbering rows, RANK for rankings, and SUM for running totals.

In data science, window functions are essential for calculating cumulative sums, moving averages, period-over-period growth, and customer ranks.

Learn in depth ↓
Module 05 · SQL · 14 concepts
In depth  /  15 lines

Window Functions, in more detail

A window function performs calculations across a defined set of rows while preserving the original row identity in the output.

Unlike GROUP BY, which collapses multiple rows into a single summary, window functions keep all individual records intact.

Every window function uses an OVER clause, which establishes the window frame or partition of rows for the calculation.

The PARTITION BY sub-clause divides the dataset into logical groups, restarting the calculation boundary for each partition.

The ORDER BY sub-clause within the OVER clause determines the sequence in which rows inside each partition are evaluated.

Ranking functions assign numerical ranks: ROW_NUMBER gives unique sequential integers, while RANK assigns tied values identical ranks.

DENSE_RANK functions like RANK but avoids leaving gaps in the ranking sequence following tied entries.

Aggregate functions can be used as window functions: writing SUM of revenue OVER ORDER BY date computes a running total.

Value navigation functions include LAG and LEAD, which retrieve values from previous or subsequent rows without performing self joins.

Using LAG allows analysts to compute period-over-period growth by comparing today revenue directly against yesterday revenue.

Window frames can be refined using the ROWS or RANGE clauses to specify moving average windows, such as a seven-day rolling average.

A rolling seven-day frame specifies ROWS BETWEEN six PRECEDING AND CURRENT ROW to smooth out weekly seasonality.

Window functions execute after WHERE, GROUP BY, and HAVING clauses, so they cannot be used inside WHERE to filter rows directly.

To filter based on a window function result, such as selecting only rank one rows, the query must be wrapped in a CTE or subquery.

Window functions eliminate expensive self joins and subqueries, making them an indispensable advanced tool for analytical SQL.

Want the full technical article? Wikipedia ↗
Test yourself

One question on this card. Every option explains itself once you pick, right or wrong.

Question 1
An analyst wants to calculate a running total of daily revenue across a calendar month. Why use a window function instead of GROUP BY?
SQL

14 concepts in this module, then a graded test with every answer explained.

Study this module →
↑↓ move↵ openesc close