SQL Practice Problems
Every problem is runnable. The schema and solution are executed in SQLite by scripts/build.py, which also generates the expected output, so every answer is verified. Use the web platform to write and auto-check your own queries in the browser.
How to practise: read the problem and schema, write your query, compare with the expected output, then read the explanation and the follow-ups (interviewers almost always ask one).
Learn the patterns first: Window functions · Advanced patterns · Performance & dialects
Easy
| # | Problem | Topics | Asked at |
|---|---|---|---|
| 01 | Second (Nth) Highest Salary | ranking, subquery, dense-rank | Meta, Amazon, Microsoft |
| 02 | Keep the Latest Record per Customer | deduplication, row-number, window-functions | Databricks, Airbnb, Netflix |
| 03 | Customers Who Never Ordered | anti-join, not-exists, nulls | Amazon, Uber, Meta |
| 04 | Daily Active Users and Stickiness | aggregation, count-distinct, self-join | Meta, Snap, Spotify |
| 05 | Running Total of Revenue per Customer | window-functions, running-total, frames | Amazon, Stripe, Shopify |
| 06 | Category Share of Revenue | window-functions, aggregation, percent-of-total | Amazon, Walmart, Instacart |
| 07 | Month-over-Month and Year-over-Year Growth | lag, window-functions, time-series | Google, Netflix, Airbnb |
| 08 | First Purchase per Customer | row-number, min-by, window-functions | DoorDash, Uber Eats, Shopify |
| 09 | Days Warmer Than the Previous Day | lag, self-join, dates | Amazon, Adobe |
| 10 | Histogram of Orders per Customer | aggregation, left-join, histogram | Meta, Twitter/X, Etsy |
Medium
| # | Problem | Topics | Asked at |
|---|---|---|---|
| 11 | 7-Day Moving Average of Revenue | moving-average, window-functions, frames | Amazon, Meta, Google, Netflix |
| 12 | Moving Average with Missing Days | moving-average, range-frame, date-spine | Uber, Airbnb, Stripe |
| 13 | Top 3 Salaries per Department | ranking, dense-rank, top-n | Amazon, Meta, Microsoft, Apple |
| 14 | Users with 3+ Consecutive Login Days | gaps-and-islands, row-number, dates | Meta, Google, LinkedIn, Uber |
| 15 | Sessionize Clickstream Events | sessionization, lag, running-sum | Google, Amazon, Spotify, Pinterest |
| 16 | Ordered Funnel Conversion | funnel, conditional-aggregation, product-analytics | Meta, Amazon, Booking.com, Shopify |
| 17 | Month-over-Month User Retention | retention, self-join, product-analytics | Meta, Spotify, Netflix, Duolingo |
| 18 | Cohort Retention Matrix | cohorts, retention, window-functions | Airbnb, Uber, Robinhood, Duolingo |
| 19 | Median Order Value per Country | median, percentiles, window-functions | Google, Airbnb, Lyft |
| 20 | New Users per Day and Cumulative User Count | running-total, first-seen, date-spine | Meta, Snap, Discord |
| 21 | Users Who Bought A and Then B Within 7 Days | self-join, sequencing, time-bounds | Amazon, Instacart, Walmart |
| 22 | Pivot Monthly Revenue into Columns | pivot, conditional-aggregation, rollup | Microsoft, Salesforce, Oracle |
| 23 | Friend Request Acceptance Rate by Day | ratios, left-join, running-total | Meta, LinkedIn |
| 24 | Apply CDC Events to Get the Current Table State | cdc, deduplication, merge-logic | Databricks, Confluent, Netflix, Stripe |
| 25 | Average Days Between Purchases | lag, time-between-events, aggregation | Amazon, Starbucks, Chewy |
| 26 | Last-Touch Marketing Attribution | attribution, as-of-join, row-number | Google, Meta, TikTok, Uber |
| 27 | Rolling 3-Month Revenue per Customer | range-frame, rolling-window, time-series | Stripe, Shopify, Adobe |
| 28 | Customers Driving 80% of Revenue | running-total, pareto, window-functions | Amazon, Salesforce, Uber |
Hard
| # | Problem | Topics | Asked at |
|---|---|---|---|
| 29 | Longest Activity Streak per User | gaps-and-islands, ranking, dates | Duolingo, Strava, Meta, Google |
| 30 | Merge Overlapping Subscription Periods | intervals, gaps-and-islands, running-max | Netflix, Spotify, Amazon, Apple |
| 31 | Peak Concurrent Sessions per Day | intervals, sweep-line, running-total | Netflix, Zoom, Twitch, AWS |
| 32 | Build an SCD Type 2 Dimension from Daily Snapshots | scd2, gaps-and-islands, data-modeling | Databricks, Snowflake, Airbnb, any data warehouse team |
| 33 | Org Chart: All Reports Under Each Manager | recursive-cte, hierarchy, graphs | Microsoft, Workday, Google, SAP |
| 34 | Forward-Fill Missing Sensor Readings | forward-fill, window-functions, time-series | Tesla, Siemens, Bloomberg, Two Sigma |
| 35 | Classify Users as New, Retained, Resurrected or Churned | growth-accounting, retention, self-join | Meta, Spotify, Snap, Robinhood |
| 36 | p95 Latency per Endpoint (Nearest-Rank) | percentiles, window-functions, observability | Datadog, Google, AWS, Cloudflare |
| 37 | Trip Cancellation Rate Excluding Banned Users | joins, ratios, filtering | Uber, Lyft, DoorDash |
| 38 | Products Frequently Bought Together | self-join, market-basket, combinatorics | Amazon, Instacart, Walmart, Target |
| 39 | Time Spent in Each Ticket Status | lead, event-log, durations | Atlassian, ServiceNow, Zendesk, Salesforce |
| 40 | Exponential Moving Average with a Recursive CTE | recursive-cte, moving-average, time-series | Two Sigma, Citadel, Robinhood, Bloomberg |
| 41 | Remove Near-Duplicate Events Within 5 Seconds | deduplication, lag, event-time | Segment, Amplitude, Meta, Snowflake |
By pattern
| Pattern | Problems |
|---|---|
| Window frames / moving averages | 05, 11, 12, 27, 28, 40 |
| Ranking / dedup / top-N | 01, 02, 08, 13, 24, 29 |
| Gaps & islands / sessionization | 14, 15, 29, 30, 32, 41 |
| Retention / cohorts / growth | 04, 17, 18, 20, 35 |
| Funnels / sequences / attribution | 16, 21, 26 |
| Intervals & time | 09, 25, 30, 31, 39 |
| Recursive CTEs | 12, 20, 33, 35, 40 |
| Data engineering specific (CDC, SCD2, dedup) | 02, 24, 32, 34, 41 |