All roadmaps
SQLEasy → Hard

SQL Window Functions Mastery

Go from basic aggregations to advanced analytical queries. Window functions are asked in almost every DE interview — master them here.

22 problems~10h
1
Running Total of Daily Saleseasysql

Your first window function: SUM() OVER (ORDER BY date) — the foundation for all running calculations.

2
First Order Date per Customereasysql

MIN() OVER (PARTITION BY customer) — partition clause basics before combining with ORDER BY.

3
Total Revenue by Regioneasysql

SUM() OVER (PARTITION BY region) — running totals within groups, the partitioned window pattern.

4
Revenue Share per Producteasysql

SUM() OVER () without PARTITION — grand total in denominator to compute % share per row.

5
Most Recent Price per Producteasysql

ROW_NUMBER() OVER (PARTITION BY ... ORDER BY date DESC) — the standard pattern to get the latest row per group.

6
Users With Consecutive Login Dayseasysql

ROW_NUMBER() minus date — the classic gap-and-island setup using row number subtraction to detect streaks.

7
Rank Employees by Salary per Departmentmediumsql

RANK() vs DENSE_RANK() vs ROW_NUMBER() — know the difference and when each is correct.

8
Second Highest Salarymediumsql

Classic interview problem: use DENSE_RANK() to handle ties correctly instead of LIMIT/OFFSET.

9
Cumulative Market Sharemediumsql

Combine SUM() window with percentage calculation — common in product analytics interviews.

10
Rolling 7-Day Average Salesmediumsql

Frame clauses: ROWS BETWEEN 6 PRECEDING AND CURRENT ROW — precise window sizing.

11
Top Spending Customer per Regionmediumsql

RANK() OVER (PARTITION BY region ORDER BY spend DESC) + outer filter — the partitioned top-N pattern.

12
Month-over-Month Revenue Growthmediumsql

LAG() OVER (ORDER BY month) — access the previous row to compute growth rates without a self-join.

13
Revenue Percentile by Categorymediumsql

NTILE() and PERCENT_RANK() — rank users/products into buckets across groups.

14
Consecutive Numbersmediumsql

LAG() / LEAD() for comparing adjacent rows — the building block for all gap-and-island problems.

15
Department Top Three Salarieshardsql

DENSE_RANK() inside a CTE — filtering the top N per partition using a ranked subquery.

16
Second Highest Salary per Departmenthardsql

DENSE_RANK() with PARTITION BY — extend the single-table rank pattern to partitioned groups.

17
Salary Percentile Bandshardsql

PERCENTILE_CONT/DISC and multiple window frames in a single query — hard-level composition.

18
Year-Over-Year Growth Ratehardsql

LAG() with PARTITION BY year + percentage formula — multi-level window composition in a real business metric.

19
Median Employee Salaryhardsql

PERCENTILE_CONT(0.5) WITHIN GROUP — ordered-set aggregate functions, a distinct syntax from standard windows.

20
Customer Longest Weekly Order Streakhardsql

Gap-and-island with ROW_NUMBER() + GROUP BY island — the advanced streak detection pattern asked at Meta.

21
User Session Detectionhardsql

Cumulative SUM() over a flag column — advanced sessionization using window over boolean to assign session IDs.

22
Monthly Cohort Retentionhardsql

Real-world retention analysis: self-join + window functions together. Asked at Amazon, Google, Meta.