Advanced SQL: Window Functions, CTEs & Analytical Queries

Window functions represent the pinnacle of analytical SQL. They allow data scientists to compute running totals, rolling averages, rankings, and lead/lag differences across partitions without messy self-joins or row collapsing.

Anatomy of a Window Function

\[ \text{FUNCTION}(\text{args}) \; \mathbf{OVER} \; (\mathbf{PARTITION \; BY} \; c_1 \; \mathbf{ORDER \; BY} \; c_2 \; [\text{FRAME}]) \]
  • OVER(): Signals that the function operates as a window calculation.
  • PARTITION BY: Divides the dataset into independent calculation buckets (similar to GROUP BY, but without collapsing rows).
  • ORDER BY: Dictates the sequence of row evaluation inside each partition.
  • FRAME: Specifies the subset of partition rows included (e.g., sliding window).

Hands-on Query: MoM Growth & Top 3 Products per Category

-- Query: Identify the Top 3 revenue-generating products per category and compute MoM growth
WITH MonthlyProductRevenue AS (
    SELECT 
        p.category_name,
        p.product_name,
        DATE_TRUNC('month', o.order_date) AS order_month,
        SUM(oi.quantity * oi.unit_price) AS monthly_sales
    FROM products p
    JOIN order_items oi ON p.product_id = oi.product_id
    JOIN orders o ON oi.order_id = o.order_id
    GROUP BY p.category_name, p.product_name, DATE_TRUNC('month', o.order_date)
),
RankedProducts AS (
    SELECT 
        category_name,
        product_name,
        order_month,
        monthly_sales,
        -- Rank within category partition
        DENSE_RANK() OVER (
            PARTITION BY category_name, order_month 
            ORDER BY monthly_sales DESC
        ) AS sales_rank,
        -- Previous month's sales via LAG
        LAG(monthly_sales, 1) OVER (
            PARTITION BY category_name, product_name 
            ORDER BY order_month
        ) AS prev_month_sales
    FROM MonthlyProductRevenue
)
SELECT 
    category_name,
    product_name,
    order_month,
    monthly_sales,
    sales_rank,
    ROUND(
        (monthly_sales - prev_month_sales) / NULLIF(prev_month_sales, 0) * 100.0, 2
    ) AS mom_growth_pct
FROM RankedProducts
WHERE sales_rank <= 3
ORDER BY category_name, order_month, sales_rank;

7-Day Moving Average in SQL

To smooth daily website traffic spikes, compute a 7-day moving average using: AVG(daily_visitors) OVER (ORDER BY visit_date ROWS BETWEEN 6 PRECEDING AND CURRENT ROW) AS rolling_7d_avg.

Frequently Asked Questions (FAQ) - Student & Interview Review

Q1. What is the difference between a Window Function and a GROUP BY aggregation?

GROUP BY collapses multiple rows into a single summary row per group. A Window Function performs calculations across a set of table rows related to the current row, but RETAINS individual row identities and returns the original row count.

Q2. What is the difference between ROW_NUMBER(), RANK(), and DENSE_RANK()?

For ties (e.g., scores: 100, 100, 90): ROW_NUMBER() arbitrarily assigns unique consecutive integers (1, 2, 3). RANK() assigns ties the same rank and skips subsequent ranks (1, 1, 3). DENSE_RANK() assigns ties the same rank without skipping numbers (1, 1, 2).

Q3. How do LAG() and LEAD() enable time-series calculations in SQL?

LAG(col, n) accesses data from n rows prior to the current row, allowing calculation of Day-over-Day (DoD) or Month-over-Month (MoM) revenue changes. LEAD(col, n) accesses future rows.

Q4. What is a Common Table Expression (CTE) and why is it preferred over subqueries?

A CTE (defined via the WITH clause) creates a named, temporary result set that exists only during query execution. CTEs dramatically improve readability, can be referenced multiple times, and support recursion for hierarchical data.

Q5. How does the window frame clause (ROWS BETWEEN) operate?

The window frame defines the exact subset of rows within the partition used for running calculations — e.g., 'ROWS BETWEEN 6 PRECEDING AND CURRENT ROW' computes a 7-day moving average.

Explore Related Data Science Tutorials & Next Steps

Broaden your statistical analysis, Python data wrangling, and production engineering expertise:

SQL Foundations for Data Science

Review fundamental SELECT queries, filtering, and table joins.

SQL Cohort Analysis & Funnels

Put window functions into practice for user retention and churn analysis.

Cloud Data Warehouses: BigQuery & Snowflake

Execute distributed analytical window queries on terabyte datasets.

130+ Data Science Interview Q&A

Solve the top 20 most frequent SQL coding challenges in technical interviews.

Share Share on Facebook Share on Twitter Share on LinkedIn Pin on Pinterest Share on Reddit

You may also like this!