SQL window functions cheatsheet
Anatomy
function(args) OVER (
PARTITION BY a, b -- restart for each group (optional)
ORDER BY c DESC, d -- order inside the group
ROWS BETWEEN 6 PRECEDING AND CURRENT ROW -- the frame (optional)
)
Window functions run after WHERE, GROUP BY and HAVING, and before ORDER BY and LIMIT. So you can window over aggregates (SUM(SUM(x)) OVER ()), but you can't filter on a window result in WHERE. Use a CTE or QUALIFY.
Ranking
| Function | Ties | Values for scores 90, 80, 80, 70 |
|---|---|---|
ROW_NUMBER() | Arbitrary unless the ORDER BY is unique | 1, 2, 3, 4 |
RANK() | Same rank, then a gap | 1, 2, 2, 4 |
DENSE_RANK() | Same rank, no gap | 1, 2, 2, 3 |
NTILE(n) | Splits rows into n buckets | for n=2: 1, 1, 2, 2 |
PERCENT_RANK() | (rank − 1) / (rows − 1) | 0, 0.33, 0.33, 1 |
Always add a tie-breaker to ROW_NUMBER (ORDER BY score DESC, id), or results change between runs.
Offsets
| Function | Returns |
|---|---|
LAG(x, n, default) | Value n rows before (default 1) |
LEAD(x, n, default) | Value n rows after |
FIRST_VALUE(x) | First value in the frame |
LAST_VALUE(x) | Last value in the frame. See the gotcha below |
NTH_VALUE(x, n) | nth value in the frame |
Frames
ROWS BETWEEN 6 PRECEDING AND CURRENT ROW: physical rows. A 7-row window.RANGE BETWEEN INTERVAL 6 DAY PRECEDING AND CURRENT ROW: by value of the ORDER BY column (support varies by engine).ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW: running total.ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING: the whole partition.
Default frame: with ORDER BY and no frame, most engines use RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW. Rows with equal ORDER BY values are peers and get the same running total. Without ORDER BY, the frame is the whole partition.
Gotchas
LAST_VALUEreturns the current row with the default frame, because the frame ends at the current row. UseROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING, orFIRST_VALUEwith the order reversed.ROWScounts rows, not days. A 7-row average over sparse daily data spans more than 7 days. Build a date spine first.COUNT(DISTINCT x) OVER (…)isn't supported in most engines. Use a self-join to a spine, or approximate sketches.AVGignores NULLs. Filled gaps should be 0, not NULL, if they mean "nothing happened".- Filtering on a window result needs a CTE/subquery, or
QUALIFYwhere supported (Snowflake, BigQuery, Databricks, DuckDB, Teradata).
Patterns in one line each
| Goal | Pattern |
|---|---|
| Latest row per key | ROW_NUMBER() OVER (PARTITION BY key ORDER BY ts DESC) = 1 |
| Top N per group | ROW_NUMBER() … <= N |
| Running total | SUM(x) OVER (ORDER BY d ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) |
| Change vs previous | x - LAG(x) OVER (ORDER BY d) |
| Share of total | x / SUM(x) OVER () |
| Islands of consecutive dates | d - ROW_NUMBER() OVER (PARTITION BY k ORDER BY d) |
| Session starts | CASE WHEN ts - LAG(ts) OVER w > gap THEN 1 END, then a running SUM |
| Fill forward | LAST_VALUE(x IGNORE NULLS) OVER (ORDER BY d ROWS UNBOUNDED PRECEDING) |
Practice
- 7-day moving average of completed trips per city
- Build an SCD Type 2 table from a change log
- Each category's share of total revenue
- Days between a customer's first and second order
- Deduplicate replayed transactions
- Three-step page funnel with step-to-step conversion
- Daily EUR rate for January, filled forward over weekends
- Flag days with abnormal trip volume
- Longest login streak per user
- Month-end account balances (a semi-additive measure)
- Month-over-month revenue growth
- Daily revenue with a month-to-date running total
- Sessionize page views with a 30-minute timeout
- Top 3 products by revenue in each category