SQL window functions perform calculations across a set of rows related to the current row — its "window" — without collapsing the result set like GROUP BY does. Every window function uses an OVER() clause to define the window of rows it operates on.
No functions match your search.
How to Use SQL Window Functions
Window functions are one of the most powerful features in SQL, letting you compute running totals, rankings, and period-over-period comparisons without subqueries or self-joins. Every SQL window function follows the same pattern: function_name() OVER (PARTITION BY ... ORDER BY ... ROWS/RANGE ...).
Step 1: Understand the OVER() Clause
The OVER() clause defines the window of rows. An empty OVER() means the entire result set. PARTITION BY department restarts the calculation for each department. ORDER BY salary DESC determines row order within each partition.
Step 2: Choose the Right Ranking Function
Use ROW_NUMBER() for a unique sequential number with no ties. Use RANK() when ties should skip numbers (1,1,3). Use DENSE_RANK() when ties should not skip numbers (1,1,2). Use NTILE(n) to divide rows into n equal buckets.
Step 3: Use LAG/LEAD for Period Comparisons
LAG(amount, 1) gives the previous row's value, perfect for month-over-month change: amount - LAG(amount, 1) OVER (ORDER BY month). LEAD(amount, 1) gives the next row's value. Both accept an optional default for when no previous/next row exists.
Step 4: Build Running Totals with ROWS BETWEEN
To compute a running total, add ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW to a SUM: SUM(amount) OVER (ORDER BY date ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW). For a 7-day moving average: AVG(amount) OVER (ORDER BY date ROWS BETWEEN 6 PRECEDING AND CURRENT ROW).
Frequently Asked Questions
Is this SQL window functions reference free?
Yes, completely free with no signup. All examples are copyable.
What is a SQL window function?
A window function performs a calculation across a set of rows related to the current row — its 'window.' Unlike GROUP BY which collapses rows, window functions preserve all rows while adding the calculated value. They are defined with an OVER() clause specifying the window.
What is the difference between RANK and DENSE_RANK?
RANK leaves gaps: if two rows tie for rank 2, the next rank is 4 (not 3). DENSE_RANK doesn't leave gaps: two rows tied for rank 2 means the next rank is 3. Use DENSE_RANK when you need consecutive rankings without gaps.
What does PARTITION BY do in a window function?
PARTITION BY divides the rows into groups (partitions) and the window function is applied within each partition independently. Example: ROW_NUMBER() OVER (PARTITION BY department ORDER BY salary DESC) numbers rows within each department separately, starting from 1 in each department.
Which databases support window functions?
All major databases: PostgreSQL (8.4+), MySQL (8.0+), SQL Server (2005+), Oracle (8i+), BigQuery, Snowflake, Redshift, DuckDB. SQLite added basic window function support in 3.25.0. MySQL 5.x does NOT support window functions.