SQL Window Functions Cheat Sheet

A window function computes over a group of rows and still keeps every row.

SQL window functions cheat sheet: window anatomy, a ROW_NUMBER, RANK and DENSE_RANK table, LAG and LEAD, and ROWS vs RANGE frames
Printed sheet title: Window functions

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:

fn100909080
ROW_NUMBER()1234
RANK()1224
DENSE_RANK()1223
NTILE(2)1122
PERCENT_RANK()0.33.331
  • 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.

123
amt102015
LAG(amt)NULL1020
LEAD(amt)2015NULL
FIRST_VALUE(amt)101010
LAST_VALUE(amt)102015

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.

dayamtRANGEROWS
1101010
2206030
2306060
340100100
-- 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.

© 2026 Vincent Charles. License: CC BY-NC-ND 4.0. You may share this page with credit. Do not sell the sheets or share modified versions.

SQL cheat sheets by Vincent Charles, from SQL Interview Questions for Data Engineers. dataengineers.club