- Home
- Courses
- SQL Interview & Case Lab
- Interview SQL Patterns
Micro Free Course — 100% Free Learning
All lessons in this module are free to learn. Sign in with Google to save your progress.
Interview SQL Patterns
The named interview classics: Nth-highest, gaps-and-islands, pivoting without PIVOT, recursive CTEs, custom date bucketing, and set operations. Memorize the shapes — these recur in every screen.
Module Content
The Nth Highest Salary Problem
The most-asked SQL interview question of all time. Five wrong answers, two right ones. FIND_VIDEO: search 'nth highest salary SQL leetcode' — recommended channel: Joma Tech / Alex Freberg. Aim for 9 min or under.
Recap — Why People Get Nth Highest Wrong
The trap, the standard answer, and how to extend it when the interviewer adds 'handle ties'.
Consecutive Days, Sessions — Gaps and Islands
The pattern behind 'consecutive logins', 'session detection', 'streaks' — one of the SQL world's named techniques. FIND_VIDEO: search 'SQL gaps and islands consecutive days' — recommended channel: Itzik Ben-Gan / Modern SQL. Aim for 12 min or under.
Recap — The Gaps and Islands Pattern
Once you see the trick, you'll see this everywhere — login streaks, session bounds, contract gaps.
Pivot Using CASE WHEN — Cross-Database Compatibility
PIVOT is vendor-specific. CASE WHEN works everywhere. Here's the canonical pattern. FIND_VIDEO: search 'SQL pivot CASE WHEN tutorial' — recommended channel: Alex Freberg / EverSQL. Aim for 9 min or under.
Recap — CASE WHEN Pivoting
Converting long-format data into wide-format columns — the universal SQL idiom.
Recursive CTEs — Org Charts and Hierarchies
The one SQL feature most analysts never use — and the one that solves 'show me all descendants' in 10 lines. FIND_VIDEO: search 'recursive CTE SQL hierarchical query' — recommended channel: Modern SQL / Itzik Ben-Gan. Aim for 11 min or under.
Recap — Building Recursive Queries
The recursive CTE pattern: base case + recursive case + termination. Same in every database.
Custom Date Buckets — Fiscal Weeks and Rolling Windows
When DATE_TRUNC isn't enough — custom fiscal calendars, rolling buckets, ISO weeks. FIND_VIDEO: search 'SQL date bucketing fiscal calendar' — recommended channel: EverSQL. Aim for 10 min or under.
Recap — Date Bucketing Recipes
The four date-bucketing patterns every BI analyst hits, with the SQL for each.
EXCEPT, INTERSECT, UNION — For Diffs and Dedup
When self-joins are overkill — the set operations every analyst should reach for. FIND_VIDEO: search 'SQL EXCEPT INTERSECT UNION ALL' — recommended channel: Modern SQL. Aim for 9 min or under.
Recap — Choosing the Right Set Op
UNION vs UNION ALL, EXCEPT vs LEFT JOIN, INTERSECT vs INNER JOIN. Same answers, different speeds and clarity.