March 2026 · 11 min read
SQL LeetCode Problems: Top Patterns & Interview Prep Guide (2026)
SQL interviews at data-focused companies test a narrow set of patterns. Master joins, window functions, CTEs, and aggregations — and 35–50 well-chosen problems is all you need to be ready for Amazon, Meta, Google, and Bloomberg.
Why SQL LeetCode Differs from Algorithm LeetCode
Algorithmic LeetCode tests problem-solving patterns — the same sliding window or BFS logic applies across many problems. SQL LeetCode is different: it's less about novel patterns and more about expressing precise logic using SQL's declarative syntax. The challenge isn't identifying what to compute — it's knowing which SQL construct expresses it cleanly: is this a subquery or a window function? A self-join or a CTE? Getting this right under interview pressure requires deliberate practice with each SQL construct, not just knowing they exist. This guide covers the 5 core patterns that appear in 90%+ of SQL interview problems, with a 4-week study plan calibrated to real company expectations. For the algorithmic side of your prep, see our guide on DSA interview questions.
Who Should Focus on SQL LeetCode
SQL proficiency is tested in these roles:
- Data engineers: Heavy SQL across all tiers — window functions and CTEs are expected
- Data scientists / ML engineers: SQL for data extraction and validation; typically beginner to intermediate
- Analytics engineers: Advanced SQL including complex CTEs, performance considerations
- Backend software engineers (data-heavy companies): Basic to intermediate SQL appears in some technical screens
Pure software engineering roles at FAANG (SWE, not data roles) rarely test SQL. If you're interviewing for a data-adjacent role at Amazon, Meta, or Bloomberg, SQL prep is non-negotiable. For general coding interview questions by company, see the linked guide.
5 Core SQL Patterns
Pattern 1: JOINs (INNER, LEFT, Self-Joins)
JOINs appear in the majority of SQL LeetCode problems. Key concepts: INNER JOIN returns only matching rows; LEFT JOIN returns all rows from the left table plus matches (nulls where no match). Self-joins are used when a table references itself — e.g., finding employees who earn more than their manager. Practice JOIN problems until you can write them without thinking. Common LeetCode problems: Combine Two Tables (easy), Employees Earning More Than Their Managers (easy), Department Top Three Salaries (hard).
Pattern 2: Aggregations + HAVING
GROUP BY with aggregate functions (COUNT, SUM, AVG, MAX, MIN) is table stakes for any SQL role. The key distinction: WHERE filters rows before aggregation; HAVING filters after. Many candidates forget this and write WHERE on an aggregate column, which fails. Practice problems: Customers Who Never Order (easy — WHERE with subquery), Count Salary Categories (medium — CASE with aggregation), Game Play Analysis (medium series).
Pattern 3: Window Functions
Window functions are the most differentiating SQL skill in data engineering interviews. The core syntax: FUNCTION() OVER (PARTITION BY col ORDER BY col2). Key functions:
- ROW_NUMBER(): Assigns sequential integers; no ties. Use for deduplication or pagination.
- RANK() / DENSE_RANK(): Same rank for ties; RANK() gaps, DENSE_RANK() doesn't.
- LAG() / LEAD(): Access the previous/next row's value. Use for trend analysis (e.g., day-over-day change).
- SUM() / AVG() OVER: Running totals and moving averages without GROUP BY collapsing rows.
LeetCode hard problems almost always require window functions. Practice: Rank Scores (easy), Department Top Three Salaries (hard), Find Median Given Frequency of Numbers (hard).
Pattern 4: Subqueries + CTEs
CTEs (Common Table Expressions) with WITH clauses make complex queries readable and are preferred in modern SQL. Correlated subqueries — where the subquery references the outer query's table — are the hardest SQL concept on LeetCode. Example: finding all employees who earn more than the average salary in their department requires a correlated subquery or a CTE + JOIN. Practice both forms; some interviewers prefer one over the other. When given the choice, use a CTE for clarity.
Pattern 5: Date and String Functions
Date manipulation appears in ~30% of medium SQL problems: DATE_FORMAT, DATEDIFF, MONTH(), YEAR(), DATE_ADD. String functions (SUBSTRING, CONCAT, LIKE, REGEXP) appear less frequently but can trip up candidates who've only practiced MySQL-agnostic SQL. Learn the MySQL-specific date functions since LeetCode defaults to MySQL dialect.
Recommended Problem List by Tier
LeetCode has ~200 SQL problems. You don't need all of them. This tiered list covers the key patterns:
- Easy (15 problems): Combine Two Tables, Customers Who Never Order, Duplicate Emails, Employees Earning More Than Their Managers, Rising Temperature, Find the Team Size, Reformat Department Table. Focus: JOINs, basic aggregation, simple subqueries.
- Medium (25 problems): Department Highest Salary, Rank Scores, Consecutive Numbers, Department Top Three Salaries, Exchange Seats, Trips and Users, Game Play Analysis IV. Focus: window functions, multi-table JOINs, date functions, CTEs.
- Hard (10 problems): Find Median Given Frequency of Numbers, Department Top Three Salaries (variant), Human Traffic of Stadium, Find Cumulative Salary of an Employee, Trips and Users. Focus: complex window queries, correlated subqueries, pivoting data.
SQL vs. Python for Data Roles
Many data roles test both SQL and Python (pandas). SQL is preferred for set-based operations on relational data; Python/pandas is preferred for row-by-row transformations and ML pipelines. In interviews, when given a choice, default to SQL for JOIN-heavy or aggregation-heavy problems — it's more readable and typically faster on large datasets. Python is preferred for complex business logic, iterative computation, or when working with unstructured data. At Amazon, data engineering interviews typically include both; at Bloomberg, SQL is weighted more heavily for analytics roles.
Company-Specific SQL Expectations
- Amazon: SQL in data science and analytics engineering roles. Medium difficulty — multi-table JOINs, aggregations, basic window functions. Leadership Principles round is separate. See Amazon's full interview process.
- Meta: Heavy window functions and aggregations for analytics and data engineering roles. Expect LAG/LEAD for trend analysis. Meta's data interviews move fast — clean, concise SQL is rewarded. See Meta's interview process.
- Google: BigQuery-flavored SQL for data engineering; standard SQL for analytics. CTEs are common. Google expects you to discuss query optimization (indexes, partitioning) alongside writing the query.
- Bloomberg: Advanced SQL for financial data queries. Expect correlated subqueries, complex aggregations, and date arithmetic. Bloomberg's SQL bar is among the highest for analytics roles. See Bloomberg's interview process.
4-Week SQL Study Plan
- Week 1: Easy problems only. 2–3 problems/day. Focus on JOINs (INNER, LEFT, self-join) and basic GROUP BY + HAVING. Goal: write any JOIN from scratch without reference.
- Week 2: Medium problems. Window functions (ROW_NUMBER, RANK, DENSE_RANK, LAG, LEAD). Do 5+ window function problems before moving on. This is the week most candidates fall behind — push through it.
- Week 3: Hard problems + CTEs. Correlated subqueries and complex multi-step queries. Rewrite your hard solutions using CTEs for readability. Practice date functions with Game Play Analysis series.
- Week 4: Mock interview mode. Time yourself (25 minutes per medium problem, 35 minutes per hard). Review your Week 1–3 problems again (spaced repetition). Add company-tagged SQL problems from LeetCode Premium if available.
Frequently Asked Questions
How many SQL LeetCode problems should I do?
35–50 problems is sufficient for most data engineering and analytics roles. Aim for 10–15 easy problems, 20–25 medium problems, and 5–10 hard problems. LeetCode has ~200 SQL problems total; you don't need all of them.
Is SQL tested at FAANG companies?
Yes, but primarily for data-focused roles. Amazon, Meta, and Google test SQL in data engineering, data science, and analytics engineering interviews. Software engineering roles at the same companies rarely require SQL. Bloomberg and other finance-adjacent companies test SQL more broadly.
What SQL topics come up most in data engineering interviews?
Window functions (ROW_NUMBER, RANK, LAG, LEAD, SUM OVER PARTITION BY) are the most differentiating topic. CTEs and correlated subqueries appear frequently in medium and hard questions. JOINs and GROUP BY + HAVING are table stakes — every candidate should have these mastered before the interview.
What is the hardest SQL concept to learn for LeetCode?
Window functions with complex PARTITION BY and ORDER BY clauses trip up most candidates. Correlated subqueries are the second hardest. Both require understanding how SQL evaluates queries logically, not just syntactically. Spend at least one full week on window functions before attempting hard problems.
Can I use PostgreSQL syntax on LeetCode SQL problems?
LeetCode SQL problems default to MySQL syntax. Some problems support PostgreSQL as well — you can select the SQL dialect in the problem settings. PostgreSQL-specific functions may not work in MySQL mode. Stick to standard SQL with MySQL-compatible syntax unless the problem explicitly supports PostgreSQL.