SQL & Relational Database Mastery · Chapter 12 · ~15 min · Senior Engineering Capstone & Technical Interview
SQL Capstone: Senior Engineering & Architecture Interview Prep
The definitive interview guide for Senior and Staff Backend & Database Engineers. Master the 4-stage SQL technical interview framework, Gaps-and-Islands algorithms, B-tree sargability, optimizer execution plans, and ACID transaction concurrency.
- 12 Deep Technical SQL Questions spanning Chapters 1 through 11
- 4-Stage Senior SQL Interview Matrix: Clarify, Outline, Code, Verify
- Interactive Arena: Gaps & Islands, Top-N Ties, and EXPLAIN Tuning
- Hands-on Bug Hunt: Consecutive Month User Retention CTE
SQL Capstone: Senior Engineering & Interview Prep: learn it visually
Solve FAANG-style Hard SQL interview questions, master gaps & islands problems, and defend relational system design trade-offs.
1The 4-Stage Senior SQL Interview Matrix
At senior and staff levels (L5+ / L6 at Google, Meta, Stripe, Amazon), SQL interviews test architectural maturity and edge-case defensiveness, not just typing valid queries. Follow the 4-Stage Framework:
Ask about duplicates, NULL semantics, scale, and tie-breaking before typing a single character.
Decompose complex logic into modular CTEs. Explain data flow to the interviewer first.
Write clean SQL with explicit aliases, sargable predicates, and optimal window frames.
Trace execution plans, analyze worst-case time complexity, and defend indexing trade-offs.
Wrong idea: Writing 5 self-joins for consecutive sequence problems
Attempting LeetCode 'Gaps & Islands' problems with n-way self-joins (a JOIN b ON a.date = b.date + 1 JOIN c...). This scales at O(N^k) and fails when sequence lengths vary.
Real system: Date math with ROW_NUMBER() creates invariant island groups
The difference between an active event date and ROW_NUMBER() (date - ROW_NUMBER()) produces an invariant constant value for consecutive days, grouping islands in a single O(N log N) pass.
2Interactive Senior SQL Technical Arena
Test your problem-solving intuition across three classic senior interview challenges: Gaps and Islands, Top-N Ties, and EXPLAIN ANALYZE index tuning:
Senior SQL Technical Interview Arena & Gaps-and-Islands Lab
Problem: Find users with 3 or more consecutive active login days. Notice how subtracting ROW_NUMBER() * 1 day from each login date yields an identical anchor date for consecutive days, grouping arbitrary streaks in a single pass.
| user_id | login_date | row_number() | anchor (date - rn) | streak state |
|---|---|---|---|---|
| 101 | 2026-10-01 | 1 | 2026-09-30 | Streak A (Day 1) |
| 101 | 2026-10-02 | 2 | 2026-09-30 | Streak A (Day 2) |
| 101 | 2026-10-03 | 3 | 2026-09-30 | Streak A (Day 3 ✓) |
| 101 | 2026-10-06 | 4 | 2026-10-02 | New Streak B (Day 1) |
| 101 | 2026-10-07 | 5 | 2026-10-02 | Streak B (Day 2) |
WITH numbered AS (
SELECT user_id, login_date,
login_date - (ROW_NUMBER() OVER (PARTITION BY user_id ORDER BY login_date) * INTERVAL '1 day') AS island_id
FROM user_logins
)
SELECT user_id, MIN(login_date) AS streak_start, MAX(login_date) AS streak_end, COUNT(*) AS streak_days
FROM numbered
GROUP BY user_id, island_id
HAVING COUNT(*) >= 3;3Category-Filtered Technical Interview Questions Bank
Filter interview questions across core relational database domains. Review evaluation rubrics, deep technical model answers, and critical candidate traps:
Trace the logical processing order of an ANSI SQL query. Why does `SELECT department, COUNT(*) AS cnt FROM employees WHERE cnt > 5 GROUP BY department` throw a syntax or binding error?
- Distinction between written lexical order (SELECT -> FROM -> WHERE) and logical execution order (FROM -> WHERE -> GROUP BY -> HAVING -> SELECT -> ORDER BY).
- Explanation that WHERE filters rows before aggregation and before SELECT assigns column aliases.
- Correction using HAVING COUNT(*) > 5 or wrapping in a CTE.
Assuming HAVING and WHERE are interchangeable, or believing SELECT executes first because it is written first.
4Hands-On Challenge: 3 Consecutive Months Active Retention
A senior product analytics query is supposed to identify long-term retained users active across at least 3 consecutive calendar months. However, the query contains three critical SQL bugs: it groups on daily truncations instead of monthly boundaries, fails to partition the window function by user ID (bleeding cross-user dates), and checks INTERVAL '1 day' instead of consecutive months!
Refactor the query below, then click Run & verify:
🎯 Find and fix the bugs in the code above, then press Run & verify to test your solution.
Need a hint?
- Group on monthly boundaries using
DATE_TRUNC('month', login_time). - Partition window lags by user:
OVER (PARTITION BY user_id ORDER BY active_date). - Validate consecutive calendar months using
INTERVAL '1 month'.
Why can the HAVING clause filter aggregated counts (e.g. HAVING COUNT(*) > 5) while the WHERE clause cannot?
Under the B-Tree Leftmost Prefix Rule, which query can utilize a composite index on (tenant_id, status, created_at) for an index range seek?
5Course Graduation & Career Next Steps
Congratulations! You've Completed SQL & Relational Database Mastery
You have mastered the complete 12-chapter curriculum: from relational foundations and logical processing, to complex joins, normalization, recursive CTEs, window functions, B-tree internals, EXPLAIN query tuning, and ACID transactions.