SQL Window Functions
Practice

30+ hands-on challenges covering every window function pattern — the #1 most-tested topic in FAANG SQL interviews. Real datasets. AI tutoring. Free to start.

Start Practicing Free →

What are SQL window functions?

A window function performs a calculation across a set of rows related to the current row — without collapsing them the way GROUP BY does. The window is defined by an OVER clause.

Example — rank employees by salary within each department
SELECT
    employee_id,
    department,
    salary,
    RANK() OVER (
        PARTITION BY department
        ORDER BY salary DESC
    ) AS salary_rank
FROM employees;

💡 This returns every row alongside each employee's salary rank within their department — something you cannot do with GROUP BY alone.

Window functions covered in this track

Ranking functions

  • ROW_NUMBER() — unique sequential number per row
  • RANK() — rank with gaps for ties
  • DENSE_RANK() — rank without gaps
  • NTILE(n) — divide rows into buckets

12 challenges

Practice ranking →

Offset functions

  • LAG(col, n) — access a previous row
  • LEAD(col, n) — access a future row
  • FIRST_VALUE(col) — first value in window
  • LAST_VALUE(col) — last value in window

8 challenges

Practice LAG/LEAD →

Aggregate windows

  • SUM() OVER — running totals
  • AVG() OVER — moving averages
  • COUNT() OVER — running counts
  • MAX() / MIN() OVER — rolling max/min

10 challenges

Practice aggregate windows →

Frame clauses

  • ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
  • ROWS BETWEEN N PRECEDING AND N FOLLOWING
  • RANGE vs ROWS frames

6 challenges

Practice frames →

Sample challenges

Easy

Running total of sales

Calculate a cumulative sum of sales per salesperson, ordered by date.

Uses: SUM() OVER (PARTITION BY ... ORDER BY ...)

Try it →
Medium

Top earner per department

Return the highest-paid employee in each department. Handle ties by returning all tied employees.

Uses: DENSE_RANK() PARTITION BY

Try it →
Medium

Month-over-month growth

Calculate the percentage change in revenue versus the previous month for each product.

Uses: LAG() percentage calculation

Try it →
Hard

7-day moving average

Calculate a rolling 7-day average of daily active users for each product.

Uses: AVG() OVER (ROWS BETWEEN 6 PRECEDING AND CURRENT ROW)

Try it →
See all 30 challenges →

Why window functions matter for interviews

Window functions appear in approximately 70% of hard SQL interview questions at Meta, Google, Amazon, and other top tech companies. Mastering them is the single highest-ROI thing you can do to prepare for a data role interview.

They're also highly practical — window functions power retention cohorts, rankings, running totals, and period-over-period comparisons that data teams run every day.

Ready to master window functions?

30+ challenges, real datasets, AI tutoring. Free to start.

Start Practicing Free →

Other challenge topics

SQL Joins →INNER, LEFT, RIGHT, FULL, self-joins. 20+ challenges. CTEs →Clean queries with WITH clauses. 15+ challenges. Subqueries →Correlated, scalar, EXISTS. 18+ challenges. Aggregation →SUM, COUNT, AVG, GROUP BY, HAVING. 25+ challenges.