Window functions
SQL Window Function Interview Questions
A window function does a calculation across a set of related rows while keeping every row in the output: a rank within each group, the previous value, a running total. It answers questions that grouping cannot, which is why it is the most tested advanced SQL topic.
What window function questions ask you to do
Across the questions here, these are the shapes that come up:
- Return the first, second or most recent row per group
- Compare each row with the one before it
- Build a running total or a moving average
- Show each row against its own group's total
- Find runs of consecutive days or events
Common mistakes with window functions
ROW_NUMBER, RANK and DENSE_RANK look interchangeable and are not. They differ only when two rows tie. ROW_NUMBER picks one of them and which one is not guaranteed, so the same query can return different rows on different runs. RANK keeps both and skips the next number. DENSE_RANK keeps both without skipping.
Questions to practice
- Salary against department totalSQL
Microsofteasy - Back to back repeatsSQL
Spotifymedium - Daily returnSQL
Coinbasemedium - Day over day swingSQLPremium
Swiggymedium - Hours between scansSQL
Amazonmedium - How often it ran outSQL
Blinkitmedium - Lineups above their contest averagePandas
FanDuelmedium - Order sequence per customerSQL
Walmartmedium - Plan changesSQL
Shopifymedium - Points above the contest averageSQL
DraftKingsmedium - Price dropsSQLPremium
Myntramedium - Price moved since last tradeSQLPremium
Zerodhamedium - Quiet time between incidentsSQL
Datadogmedium - Salary rank within departmentSQL
Netflixmedium - Three day averageSQL
Coinbasemedium - Best and worst day per assetPandas
Robinhoodhard - Compare with the previous orderSQL
Best Buyhard - Each user's top categorySQLPremium
Pinteresthard - Every stockout windowSQLPremium
Blinkithard - Failures in a rowSQLPremium
PhonePehard - Listening sessionsSQLPremium
Spotifyhard - Longest price holdSQLPremium
Myntrahard - Longest rising streakSQL
Coinbasehard - Longest stockout runSQL
Blinkithard - Merged outage windowsSQL
Datadoghard - Month-over-month spend changeSQLPremium
Visahard - Most-watched title in each countrySQLPremium
Netflixhard - Posting on consecutive daysSQLPremium
Metahard - Retries before it workedSQLPremium
PhonePehard - Running revenue over timeSQL
Stripehard - Top restaurant in each citySQLPremium
DoorDashhard - Unbroken runs of monthsSQL
Shopifyhard
Questions last reviewed 19 October 2026.