Modules / Database / Window Functions

SQL Window Functions

Visualize how calculations are performed across a set of table rows that are somehow related to the current row.

Data Preview (Source + Result)

READY

Overview

GROUP BY combines rows into one row per group. A window function instead calculates across a window of related rows and returns a value for each original row. This makes it useful for ranks, running totals, moving averages, and comparing a row with its neighbors.

Aggregate without collapsing rows

query.sqlSQLite
Result

PARTITION BY divides rows into independent groups; ORDER BY defines their sequence inside each group. The window does not remove rows.

Ranking and neighboring rows

ROW_NUMBER assigns a unique sequence within a partition. RANK leaves gaps after ties; DENSE_RANK does not. LAG and LEAD read a value from a previous or next row in the ordered partition. Include a deterministic tie-breaker when the order must be stable.

Frames can change the answer

A window frame defines which ordered rows contribute to the current calculation. Defaults depend on the function and ordering and can include peers with equal order values. For a running total, specify a frame deliberately when peer behavior matters. For LAST_VALUE over the entire partition, a frame ending at the current row often surprises; request UNBOUNDED FOLLOWING when you mean the partition's final value.

Window functions run after WHERE, grouping, and HAVING in the logical query model. To filter by a window result, put the calculation in a subquery or CTE and filter outside it. See query execution order.

Running values versus whole-group values

With PARTITION BY customer_id and an order, a cumulative frame produces a running total that changes row by row. To show each order alongside the customer's full total, use the same partition but omit the ordering, or specify a frame that covers the whole partition. Ranking has no frame requirement in the same way, but ties still matter. Verify a partition with duplicate sort values, a single row, and NULLs so the result matches the intended report rather than a default frame assumption.

Filter the result in an outer query

A window result is calculated after WHERE, so this form cannot generally filter rows by a rank alias in the same query block. Put the rank in a CTE or subquery, then filter the outer result to keep the top rows per partition. Include a tie policy: ROW_NUMBER selects a fixed number of rows, while RANK may return more rows when ranks tie. The choice changes whether “top three” means exactly three rows or every row in the top three rank positions.

Further reading

Predict, then reveal

About to run: switch Load Dataset to Weather Data. Before it does — what happens to the readout?

Committing to an answer first is the point — the reveal runs the experiment on the visualisation above and reads the real value back, so nothing here is scripted.

Recall check

0 of 3

Say the answer out loud before you reveal it — recalling it is what makes it stick, and rereading it is not.

  1. What does this module say about “Aggregate without collapsing rows”?

  2. What does this module say about “Ranking and neighboring rows”?

  3. What does this module say about “Frames can change the answer”?

Cheat sheet

SQL Window Functions

Rank, total and compare across rows without collapsing them, using OVER and PARTITION BY on a live result set.

DATABASE · vizlearn.in/database/window_functions_in_sql.html

Further reading

About the author

Ashish Jangra builds and maintains VizLearn. Every module here is written and the visualisation behind it hand-built, so the numbers in a readout come from the same code that draws the picture. Corrections are genuinely welcome and get priority over everything else — if a page states something wrong, or an animation misrepresents what the algorithm does, get in touch.