Chapter 8 · Window Functions
SQL Window Functions Practice
Window functions compute a value for each row using other rows around it, without collapsing them like GROUP BY does. They answer the questions that are hard any other way: top N per group, running totals, month-over-month change.
They're the most tested advanced SQL topic in data analyst and data engineer interviews, and the one that separates beginners from confident SQL users.
Key ideas
- OVER (PARTITION BY … ORDER BY …)
- Defines the window: which rows belong together and in what order.
- ROW_NUMBER, RANK, DENSE_RANK
- Number rows within each group. They differ only in how ties are handled.
- LAG / LEAD
- Read the previous or next row's value: perfect for period-over-period change.
- Running totals
- SUM(x) OVER (ORDER BY date) adds up values row by row.
Example: Top 3 most expensive products in each category
SELECT *
FROM (
SELECT name, category_id, price,
RANK() OVER (PARTITION BY category_id
ORDER BY price DESC) AS price_rank
FROM products
) ranked
WHERE price_rank <= 3;RANK restarts at 1 for every category; the outer query keeps the first three of each.
In interviews
"Top N per group" is the single most common advanced SQL interview question. Know it with ROW_NUMBER and with RANK, and explain which one you'd pick when there are ties.
All 62 Window Functions exercises
Ordered from easiest to hardest. Each one runs on a real database with instant feedback.
- 01Rank Products by Price Within CategoryecommerceMedium
- 02Customer Order Sequence NumbersecommerceMedium
- 03Rank Products by Revenue Within CategoryecommerceMedium
- 04Running Total of PaymentsecommerceMedium
- 05Days Between Consecutive Customer OrdersecommerceMedium
- 06Category Share of Total RevenueecommerceMedium
- 07Cumulative Monthly RevenueecommerceMedium
- 08Cumulative Loan Disbursements by MonthfinanceMedium
- 09Rank Doctors by Salary Within SpecialtyhealthcareMedium
- 10Cumulative Billing ReporthealthcareMedium
- 11Doctor Salary Quartiles with NTILEhealthcareMedium
- 12Compare Each Bill to the Previous OnehealthcareMedium
- 13Dense Rank Patients by Visit FrequencyhealthcareMedium
- 14Monthly Revenue with Month-over-Month ChangehealthcareMedium
- 15Running Total of Daily RevenueecommerceHard
- 16Compare Order Value to Previous OrderecommerceHard
- 17Dense Rank Customers by Total SpendingecommerceHard
- 183-Month Moving Average of Order ValueecommerceHard
- 19Price Rank per Category IDecommerceHard
- 20Running Total of Daily Order RevenueecommerceHard
- 21Customer Order Sequence NumberecommerceHard
- 22Top 3 Products per Category by RevenueecommerceHard
- 237-Day Moving Average of Order AmountsecommerceHard
- 24Rank Customers by Order Value with RANK and DENSE_RANKecommerceHard
- 25Rank Accounts by Balance Within TypefinanceHard
- 26Running Transaction Total Per AccountfinanceHard
- 27Identify Duplicate Customer EmailsfinanceHard
- 28Previous Transaction AmountfinanceHard
- 29Account Balance QuartilesfinanceHard
- 30Dense Rank Customers by Total BalancefinanceHard
- 31Each Account as Percentage of Customer TotalfinanceHard
- 32Next Payment Date for Each Loan PaymentfinanceHard
- 333-Transaction Moving AveragefinanceHard
- 34Percent Rank of Account BalancesfinanceHard
- 35Highest Balance Account per TypefinanceHard
- 36Top 3 Accounts by Balance Per TypefinanceHard
- 37Detect Payment Gaps Over 60 DaysfinanceHard
- 38Account Balance Quartile ClassificationfinanceHard
- 39Running Balance After Each TransactionfinanceHard
- 40Dense Rank Transactions by Amount per Account TypefinanceHard
- 41Rank Customers by Net WorthfinanceHard
- 42Detect Three Consecutive DepositsfinanceHard
- 43Year-over-Year Account Balance GrowthfinanceHard
- 44Per-Customer 3-Transaction Moving AveragefinanceHard
- 45Account Balance Volatility RankingfinanceHard
- 46NTILE Customer SegmentationfinanceHard
- 47Rank Doctors by Salary Within DepartmenthealthcareHard
- 48Running Total of Billing AmountshealthcareHard
- 49Days Between Consecutive VisitshealthcareHard
- 50Top Billing Doctor per DepartmenthealthcareHard
- 517-Day Moving Average of Daily BillinghealthcareHard
- 52Latest Visit per Patient Using ROW_NUMBERhealthcareHard
- 53Percentile Rank of Doctor SalarieshealthcareHard
- 54Moving Average of Daily Bill TotalshealthcareHard
- 55Highest and Lowest Paid Doctor per DepartmenthealthcareHard
- 56Cumulative Visit Count per Doctor Over TimehealthcareHard
- 57Days Until Next Visit per PatienthealthcareHard
- 58Each Doctor's Share of Department Salary BudgethealthcareHard
- 59Rank Doctors by Patient Count Within Each DepartmenthealthcareHard
- 60Visit Gap Analysis with Window FunctionshealthcareHard
- 61Doctor Workload Percentile RankinghealthcareHard
- 62Cumulative Abnormal Lab Results per Patient Over TimehealthcareHard