← Back to All Articles
SQL • Tutorial

10 Essential SQL Window Functions Every Data Analyst Must Know

By Dibya Lochan Mendali • Updated November 26, 2026 • 7 min read

TL;DR — Why Window Functions Matter

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-01EastAnanya42000
2026-01-01EastRohan42000
2026-01-01EastMeera35000
2026-02-01EastAnanya51000
2026-02-01WestKabir60000

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):

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()RankingDeduplicating records & picking Top-1 per group
RANK()RankingCompetition-style leaderboards with rank gaps
DENSE_RANK()RankingFinding Top-N distinct salaries or revenue tiers
NTILE(n)DistributionSplitting customers into quartiles or deciles (RFM)
LAG()OffsetComparing current period vs. previous period
LEAD()OffsetCalculating days until next purchase or churn event
FIRST_VALUE()OffsetComparing every row against a user's initial order
LAST_VALUE()OffsetGrabbing latest status in an event log
SUM() OVER()AggregateCumulative running totals & % of total contribution
AVG() OVER()AggregateRolling 7-day or 30-day smoothing averages
Pro Tip: Filtering Window Function Results

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.

Written by Dibya Lochan Mendali

Self-taught Data Analyst & Web Developer from Khaupali, Odisha. Sharing practical SQL, Power BI, Python & Excel workflows.

Explore Data Projects →