SQL Question Bank — Master Tracking File
💡 Interview Tip
HOW TO USE THIS FILE:
- This is the SINGLE SOURCE OF TRUTH for all SQL interview questions
- Every question has a Q# (never change old numbers, only append new ones)
- Pattern# maps to the 15 patterns in the prep files
- To ADD new questions: append rows at the bottom with the next Q#
- To CLASSIFY a question you received in interview: match to the Pattern column
PATTERN REFERENCE (Quick Lookup)
| P# | Pattern Name | Key SQL |
|---|---|---|
| P1 | Ranking / Top-N Per Group | ROW_NUMBER, RANK, DENSE_RANK + PARTITION BY |
| P2 | Running Totals / Cumulative | SUM() OVER (ORDER BY) |
| P3 | LAG / LEAD — Row-over-Row | LAG(), LEAD() |
| P4 | Gaps & Islands | date - ROW_NUMBER() island grouping |
| P5 | Sessionization | LAG() gap detection + cumulative SUM as session_id |
| P6 | Deduplication | ROW_NUMBER() PARTITION BY, keep rank = 1 |
| P7 | Self-Join | Same table aliased twice |
| P8 | Cross Join — Combinations | CROSS JOIN for Cartesian product |
| P9 | Market Basket / Co-occurrence | Self-join on order_id, item1 < item2 |
| P10 | Conditional Aggregation / Pivot | SUM(CASE WHEN ...) |
| P11 | Cohort / Retention Analysis | MIN(event_date) as cohort + date offset |
| P12 | Date Arithmetic | DATEDIFF, DATE_TRUNC, INTERVAL |
| P13 | Recursive CTE | WITH RECURSIVE anchor + recursive step |
| P14 | Median / Percentile | PERCENTILE_CONT or ROW_NUMBER median trick |
| P15 | Funnel Analysis | Multi-stage COUNT DISTINCT + conversion rate |
FULL QUESTION BANK
| Q# | Question | Company | Pattern | Difficulty | Solved? |
|---|---|---|---|---|---|
| Q01 | Top 2 highest-grossing products within each category in 2022 | Amazon | P1 | Medium | ✓ |
| Q02 | Top 3 salaries in each department | FAANG Classic | P1 | Medium | ✓ |
| Q03 | Email activity rank per user (sent + received + spam, dense ranked) | P1 | Medium | ✓ | |
| Q04 | Top 2 users per compan |