# SQL Interview Challenges

> Solving timed SQL problems on joins, aggregation, window functions and dates, and talking through your approach clearly. Free notes, key concepts, resources...

Canonical: https://www.internhack.in/roadmaps/tracks/data-analyst/sql-interviews
Last updated: 2026-11-28

Solving timed SQL problems on joins, aggregation, window functions and dates, and talking through your approach clearly.

Intermediate · about 15 hours · difficulty 3/5

Learn first: [Window Functions](https://www.internhack.in/roadmaps/tracks/data-analyst/sql-window-functions), [Dates, Times and Text in SQL](https://www.internhack.in/roadmaps/tracks/data-analyst/sql-dates-strings)

Nearly every analyst interview includes live or timed SQL. The questions repeat a small set of patterns, so deliberate practice pays off quickly.

### Patterns to master

- **Top N per group**: `ROW_NUMBER() OVER (PARTITION BY category ORDER BY revenue DESC)` then filter `rn <= 3`.
- **Running totals and moving averages** with window frames.
- **Period over period**: `LAG()` for month-over-month growth.
- **Gaps and islands / consecutive days**: subtract a row number from the date to group streaks.
- **Anti-joins**: customers with no orders using `LEFT JOIN ... WHERE o.id IS NULL` or `NOT EXISTS`.
- **Conditional aggregation**: `SUM(CASE WHEN ... THEN 1 ELSE 0 END)` to pivot.
- **Retention and funnels**: first event per user, then join back.
- **Deduplication**: keep the latest row per key.

### How to approach a question live

1. **Clarify**: grain of each table, duplicates, nulls, ties, time zone.
2. **Describe the plan** out loud before typing.
3. Build step by step with **CTEs**, checking intermediate results.
4. **Test edge cases**: empty groups, ties, nulls.
5. Mention **performance** if tables are large.

### Practice routine

Solve two or three problems daily for a few weeks, mixing easy and medium. Time yourself (15-20 minutes each), and rewrite solutions you struggled with a few days later from scratch.

**Pitfall:** silently writing a huge query and debugging it in panic. Narrate and build in small pieces. You are ready to move on when you can solve medium window-function problems in under 20 minutes while explaining your reasoning.

## Key concepts

- **Top N per group**
- **Running totals and moving averages**
- **LAG and period-over-period**
- **Gaps and islands**
- **Anti-joins and NOT EXISTS**
- **Conditional aggregation**
- **Deduplication patterns**
- **Thinking aloud and clarifying**

## Mini project

Solve 40 SQL problems over four weeks across the eight patterns, keep a notebook of each pattern with your cleanest solution, and do two mock interviews with a friend.

## Check yourself

Write, from memory, a query returning each customer's second most recent order.

## Free resources

- [HackerRank SQL practice](https://www.hackerrank.com/domains/sql) (HackerRank)
- [LeetCode database problems](https://leetcode.com/problemset/database/) (LeetCode)
- [SQLBolt interactive lessons](https://sqlbolt.com/) (SQLBolt)
- [Mode SQL Tutorial](https://mode.com/sql-tutorial) (Mode)

Previous: [Resume, LinkedIn and the Analyst Job Search](https://www.internhack.in/roadmaps/tracks/data-analyst/job-search) · Next: [Business Case and Analytics Case Interviews](https://www.internhack.in/roadmaps/tracks/data-analyst/case-interviews)

## More in Interview Preparation

- [SQL Interview Challenges](https://www.internhack.in/roadmaps/tracks/data-analyst/sql-interviews)
- [Business Case and Analytics Case Interviews](https://www.internhack.in/roadmaps/tracks/data-analyst/case-interviews)
- [Statistics and Experimentation Interview Questions](https://www.internhack.in/roadmaps/tracks/data-analyst/stats-product-interviews)
- [Take-Home Assignments and Presentation Rounds](https://www.internhack.in/roadmaps/tracks/data-analyst/take-home-assignments)

## Related

- [Roadmaps](https://www.internhack.in/roadmaps)
- [Data Analyst Roadmap](https://www.internhack.in/roadmaps/tracks/data-analyst)
