10 Essential SQL Window Functions Every Data Analyst Must Know
By Dibya Lochan Mendali • Updated November 26, 2026 • 7 min read
Unlike GROUP BY, which collapses rows into a single summary row, SQL window functions (OVER()) calculate rankings, moving averages, and period-over-period comparisons while keeping every individual row intact.
1. The Sample Business Dataset
Suppose you are analyzing a monthly_sales table tracking regional revenue across an e-commerce store:
| sale_month | region | rep_name | revenue |
|---|---|---|---|
| 2026-01-01 | East | Ananya | 42000 |
| 2026-01-01 | East | Rohan | 42000 |
| 2026-01-01 | East | Meera | 35000 |
| 2026-02-01 | East | Ananya | 51000 |
| 2026-02-01 | West | Kabir | 60000 |
2. Ranking Functions: ROW_NUMBER, RANK & DENSE_RANK
Interviewers frequently test the difference between these three functions when handling ties (notice Ananya and Rohan both have 42000 in January):
ROW_NUMBER(): Assigns a unique sequential integer (1, 2, 3) even when values tie.RANK(): Gives tied rows the same rank, then skips the next number (1, 1, 3).DENSE_RANK(): Gives tied rows the same rank without gaps (1, 1, 2).
SELECT
sale_month,
region,
rep_name,
revenue,
ROW_NUMBER() OVER (PARTITION BY sale_month, region ORDER BY revenue DESC) AS row_num,
RANK() OVER (PARTITION BY sale_month, region ORDER BY revenue DESC) AS rnk,
DENSE_RANK() OVER (PARTITION BY sale_month, region ORDER BY revenue DESC) AS dense_rnk
FROM monthly_sales;
3. Month-over-Month Growth with LAG() and LEAD()
LAG() looks backward at a previous row without needing a messy self-join, making it the cleanest way to calculate Month-over-Month (MoM) revenue growth:
WITH rep_monthly AS (
SELECT
rep_name,
sale_month,
SUM(revenue) AS current_rev
FROM monthly_sales
GROUP BY rep_name, sale_month
)
SELECT
rep_name,
sale_month,
current_rev,
LAG(current_rev) OVER (PARTITION BY rep_name ORDER BY sale_month) AS prev_month_rev,
ROUND(
(current_rev - LAG(current_rev) OVER (PARTITION BY rep_name ORDER BY sale_month)) * 100.0
/ NULLIF(LAG(current_rev) OVER (PARTITION BY rep_name ORDER BY sale_month), 0),
2
) AS mom_growth_pct
FROM rep_monthly;
4. Running Totals & 3-Month Moving Averages
Use ROWS BETWEEN inside your OVER() clause to control the exact sliding frame of rows included in your aggregation:
SELECT
sale_month,
region,
revenue,
-- Cumulative YTD Running Total
SUM(revenue) OVER (
PARTITION BY region
ORDER BY sale_month
ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
) AS running_total,
-- 3-Period Moving Average
ROUND(AVG(revenue) OVER (
PARTITION BY region
ORDER BY sale_month
ROWS BETWEEN 2 PRECEDING AND CURRENT ROW
), 2) AS moving_avg_3m
FROM monthly_sales;
5. Quick Cheat Sheet of All 10 Window Functions
| Function | Category | Primary Data Analyst Use Case |
|---|---|---|
ROW_NUMBER() | Ranking | Deduplicating records & picking Top-1 per group |
RANK() | Ranking | Competition-style leaderboards with rank gaps |
DENSE_RANK() | Ranking | Finding Top-N distinct salaries or revenue tiers |
NTILE(n) | Distribution | Splitting customers into quartiles or deciles (RFM) |
LAG() | Offset | Comparing current period vs. previous period |
LEAD() | Offset | Calculating days until next purchase or churn event |
FIRST_VALUE() | Offset | Comparing every row against a user's initial order |
LAST_VALUE() | Offset | Grabbing latest status in an event log |
SUM() OVER() | Aggregate | Cumulative running totals & % of total contribution |
AVG() OVER() | Aggregate | Rolling 7-day or 30-day smoothing averages |
You cannot filter a window function directly inside a WHERE clause (e.g., WHERE ROW_NUMBER() OVER(...) = 1 will throw an error) because window functions execute after WHERE and GROUP BY. Wrap your query in a CTE (WITH ranked AS (...)) and filter in the outer SELECT.
Self-taught Data Analyst & Web Developer from Khaupali, Odisha. Sharing practical SQL, Power BI, Python & Excel workflows.