SQL interview questions for data analysts
Write queries on grocery delivery data: orders, customers, events and payments. Each question includes input tables and expected output. Open the exercise to run PostgreSQL in your browser and check your answer, free without an account.
20 questions · Easy to hard · PostgreSQL in your browser
Easy SQL questions
- Second-highest price in each groupEasy · Nth-highest value
- Running revenue total by cityEasy · Running total
- Find duplicate events to deleteEasy · Duplicates
Medium SQL questions
- Top 3 categories in each cityMedium · Top-N per group
- Day-1 retention by channelMedium · Retention
- Find the bugs in a churn queryMedium · Code review
- Find the bugs in a revenue queryMedium · Code review
- Products with no salesMedium · Anti-join: what’s missing
- Time between consecutive purchasesMedium · Compare with previous
- Median delivery time by groupMedium · Median and percentiles
- Pivot monthly category revenueMedium · Pivot and unpivot
- Category pairs bought togetherMedium · Pairs and self-joins
- Recursive employee hierarchyMedium · Hierarchies and recursion
- Customers who bought every categoryMedium · Relational division
- A/B test conversion rate by platformMedium · A/B tests
- Products that make 80% of revenueMedium · Running total
Hard SQL questions
- Longest consecutive order-day streakHard · Consecutive streaks
- Weekly cohort retention tableHard · Retention
- Sessionization from event gapsHard · Time intervals
- 30-day customer lifetime valueHard · Product metrics
How to practice for your interview
- Start with an easy question. State what one output row represents before choosing joins and aggregates.
- Move to medium questions for ranking, running totals and date logic. Check ties, missing rows and period boundaries against the example.
- Try a hard question or a query to fix. Explain where a join could multiply rows and how you would verify the result.
Check more than the sample
Compare the output columns and values first. Then add a case the example does not cover: a NULL, a tied value or a customer with no matching order.
Read the shared data rules for dates, delivered orders and output conventions.