SQL Window Functions Cheat Sheet
A window function computes over a group of rows and still keeps every row.

What the sheet covers
PARTITION BY, ORDER BY and the frame (UNBOUNDED PRECEDING, CURRENT ROW, and the rest). Windows run after GROUP BY and HAVING, so SUM(SUM(x)) OVER () is valid. ROW_NUMBER, RANK, DENSE_RANK, NTILE and PERCENT_RANK; LAST_VALUE stops at the current row unless you write the frame (on Snowflake, FIRST_VALUE, LAST_VALUE and NTH_VALUE with no frame use the whole partition). With ORDER BY and no frame, aggregates default to RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW. RANGE uses the ORDER BY value, so ties share a frame. For running totals, write ROWS and make ORDER BY unique. QUALIFY is the window filter in DuckDB, Snowflake, BigQuery and Databricks.
Topics on the sheet
- Anatomy
- Ranking
- Offset and value functions
- ROWS vs RANGE
- Aggregate windows
- Filter on a window: QUALIFY
Download the PDF
Window functions as a one-page PDF in Letter and A4, plus all six sheets in one SQL cheat sheet PDF. Free PDF, download it right after you sign up. Signing up joins the dataengineers.club newsletter and unlocks the PDF bundle in Letter and A4. Unsubscribe anytime.
Anatomy
SUM(amount) OVER (
PARTITION BY customer_id
ORDER BY ts
ROWS BETWEEN 2 PRECEDING
AND CURRENT ROW
) AS last_3_sum- PARTITION BY splits rows into groups. Rows are kept, unlike GROUP BY.
- ORDER BY sets the order inside each group. Ranking and LAG/LEAD need it.
- Frame: the rows the function sees. Bounds: UNBOUNDED PRECEDING, n PRECEDING, CURRENT ROW, n FOLLOWING, UNBOUNDED FOLLOWING.
Windows run after GROUP BY and HAVING (SQL order of execution), so SUM(SUM(x)) OVER () is valid.
Ranking
fn OVER (ORDER BY score DESC) on 4 scores:
| fn | 100 | 90 | 90 | 80 |
|---|---|---|---|---|
| ROW_NUMBER() | 1 | 2 | 3 | 4 |
| RANK() | 1 | 2 | 2 | 4 |
| DENSE_RANK() | 1 | 2 | 2 | 3 |
| NTILE(2) | 1 | 1 | 2 | 2 |
| PERCENT_RANK() | 0 | .33 | .33 | 1 |
- ROW_NUMBER breaks ties in any order: add a tiebreak column.
- RANK leaves a gap after a tie, DENSE_RANK does not.
Offset and value functions
All with OVER (ORDER BY day). LAST_VALUE returns the current row (or its last tie): the default frame stops there. Exception: Snowflake with no frame uses the whole partition (15, 15, 15). Write the frame explicitly: ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING gives 15, 15, 15. Forget PARTITION BY and LAG may read the previous user's row.
| 1 | 2 | 3 | |
|---|---|---|---|
| amt | 10 | 20 | 15 |
| LAG(amt) | NULL | 10 | 20 |
| LEAD(amt) | 20 | 15 | NULL |
| FIRST_VALUE(amt) | 10 | 10 | 10 |
| LAST_VALUE(amt) | 10 | 20 | 15 |
ROWS vs RANGE
With ORDER BY and no frame, aggregate functions default to RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW in every engine here. On Snowflake, FIRST_VALUE, LAST_VALUE and NTH_VALUE with no frame use the whole partition. Without ORDER BY: the whole partition. RANGE uses the ORDER BY value, so ties share a frame. For running totals, write ROWS and make ORDER BY unique.
| day | amt | RANGE | ROWS |
|---|---|---|---|
| 1 | 10 | 10 | 10 |
| 2 | 20 | 60 | 30 |
| 2 | 30 | 60 | 60 |
| 3 | 40 | 100 | 100 |
-- last 7 days by date, not rows
SUM(amt) OVER (
ORDER BY day
RANGE BETWEEN INTERVAL '6 days'
PRECEDING AND CURRENT ROW)Interval RANGE: DuckDB, PostgreSQL 11+, Snowflake. BigQuery: numeric key. Not SQL Server.
Aggregate windows
-- running total per customer
SUM(amount) OVER (
PARTITION BY customer_id
ORDER BY ts, order_id
ROWS UNBOUNDED PRECEDING)
-- 7-row moving average
AVG(amount) OVER (
ORDER BY ts, order_id
ROWS 6 PRECEDING)
-- share of the customer's total
amount / SUM(amount) OVER (
PARTITION BY customer_id)Shares of integers: multiply by 1.0 first (NULL handling). Use NULLIF on a zero total.
Filter on a window: QUALIFY
SELECT *
FROM orders
QUALIFY ROW_NUMBER() OVER (
PARTITION BY customer_id
ORDER BY ts DESC, order_id
) = 1;DuckDB, Snowflake, BigQuery, Databricks. Elsewhere: rn in a subquery, then WHERE rn = 1.
Engines named on the sheet
DuckDB, PostgreSQL, Snowflake, BigQuery, SQL Server, Databricks. QUALIFY is noted for DuckDB, Snowflake, BigQuery and Databricks. Interval RANGE is noted for DuckDB, PostgreSQL 11+ and Snowflake; BigQuery uses a numeric key; not SQL Server. Snowflake's default frame for FIRST_VALUE, LAST_VALUE and NTH_VALUE is also noted. DuckDB 2.0.1 is the runtime used in the book. Do not read this list as a claim that every engine was executed for every example.
FAQ
What is the difference between ROW_NUMBER, RANK and DENSE_RANK?
On the scores 100, 90, 90, 80: ROW_NUMBER gives 1, 2, 3, 4, RANK gives 1, 2, 2, 4 and DENSE_RANK gives 1, 2, 2, 3. RANK leaves a gap after a tie, DENSE_RANK does not, and ROW_NUMBER breaks ties in any order unless you add a tiebreak column.
Why does LAST_VALUE return the current row?
The default frame stops at the current row (or its last tie). Write ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING to get the last value of the partition. On Snowflake, FIRST_VALUE, LAST_VALUE and NTH_VALUE with no frame already use the whole partition.
What is the default window frame?
With ORDER BY and no frame, aggregate functions default to RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW. RANGE uses the ORDER BY value, so ties share a frame. Without ORDER BY, the frame is the whole partition. For running totals, write ROWS and make ORDER BY unique, for example ORDER BY ts, order_id.
How do I filter on a window function?
Use QUALIFY in DuckDB, Snowflake, BigQuery and Databricks. Elsewhere, compute the window in a subquery and filter with WHERE in the outer query.