r/learnSQL • • 1d ago

Understanding SQL patterns that cover 90% of interview questions

Instead of trying to memorize 100 different questions, it helps to learn the underlying patterns. I put together a walkthrough of the 17 patterns I see most often in MySQL interviews, breaking down the logic so you can recognize them instantly.

The core patterns covered:

  • Window Functions: Running totals, LAG for YoY/MoM growth, and 7-day moving averages.
  • CTEs & Recursion: Traversing hierarchies (like employee/manager trees).
  • Gaps & Islands: Finding consecutive login days (a classic tough one).
  • Ranking: Top N per group, 2nd/Nth highest salary.
  • Advanced Joins: Self-joins, monthly cohort retention, and pivot tables using CASE WHEN.

Why this approach helps:
When you see a question like "find the highest sales day," you shouldn't just start typing. You should recognize it as a "Ranking + Filtering" pattern. Once you know the pattern, the query writes itself.

I recorded a step-by-step video solving each of these 17 patterns with the exact queries and explanations of why they work. I also included the dataset/script so you can practice alongside it.

Walk-through:- SQL Interview Questions & Answers MySQL 17 Must Know Patterns

(Note: I'm sharing this because I've seen it help folks in my training sessions—if it helps even one person land a role, I'm happy!)

Question for the community: Which of these patterns trips you up the most in interviews? For me, it was always the "Gaps and Islands" problem.

40 Upvotes

1 comment sorted by

4

u/Discordbothost 23h ago

The 7-day average has a gotcha if the shop has no sales on Sundays. Seven rows can span more than seven days. If quiet days count as zero, I'd fill in the missing dates first and then apply the window to one row per date.