20 SQL Interview Questions You Must Practice Before Your Next Data Interview
Preparing for a SQL interview? This guide walks you through 25 realistic query patterns – from filtering and joins to window functions and time-series analytics – each mapped to a hands-on QueryLab problem so you can practice with real data, not just theory.
If you're preparing for data analyst, data engineer, or backend interviews, you don't need an endless list of random SQL puzzles.
You need to master the patterns that actually show up:
- Filtering and joins on realistic schemas
- Aggregations and
GROUP BYedge cases - Window functions for rankings and running totals
- Time-series questions like rolling averages
- Real-world business logic using
CASE, subqueries, andEXISTS
This guide gives you 25 questions to practice – each tied to a concrete, interactive problem in TheQueryLab catalog. Use it as a structured checklist before your next interview.
How to use this guide:
- Read the question and what the interviewer is really testing.
- Open the linked QueryLab problem and solve it in SQL.
- Rerun the query with edge cases until you're confident you could code it live in an interview.
1. Basic filtering: Recyclable and Low Fat Products
Pattern: simple WHERE filtering with multiple boolean conditions.
Interview goal: check if you can translate a clear business rule into a clean SELECT with AND logic.
Practice on TheQueryLab: Solve "Recyclable and Low Fat Products" on TheQueryLab.
2. Filtering with NULLs: Find Customer Referee
Pattern: filtering with NULL and inequality.
Interview goal: see whether you really understand how NULL interacts with = and <>, and whether you reach for IS NULL / IS NOT NULL.
Practice on TheQueryLab: Solve "Find Customer Referee" on TheQueryLab and write a query that excludes customers referred by a specific user while still including customers with NULL referee_id.
3. Aggregation warm-up: Monthly Revenue by Category
Pattern: GROUP BY + date truncation + ordering.
Interview goal: validate that you can aggregate by a derived month key instead of grouping by the raw date.
Practice on TheQueryLab: open the SQL Interview Preparation Kit and Solve the "Sales revenue per category" problem by computing total_revenue per (category, month) using DATE_TRUNC or TO_CHAR.
4. Second highest value: 2nd Highest Salary
Pattern: subqueries + DISTINCT + LIMIT/OFFSET.
Interview goal: evaluate whether you can return a single scalar value, handle ties, and correctly return NULL when the second distinct salary doesn't exist.
Practice on TheQueryLab: Solve "2nd Highest Salary" using a scalar subquery with DISTINCT and LIMIT 1 OFFSET 1.
5. Ranking without gaps: Rank Scores
Pattern: window functions for ranking.
Interview goal: check that you know when to use DENSE_RANK() instead of RANK() and can explain the difference in terms of gaps.
Practice on TheQueryLab: Solve "Rank Scores" and assign ranks using DENSE_RANK() over ORDER BY score DESC.
6. Top N per group: Cities With Most Trades
Pattern: join + aggregation + ORDER BY with ties.
Interview goal: test if you can combine joins and aggregates to rank entities, and handle tie-breaking correctly.
Practice on TheQueryLab: Solve "Cities w/ Most Trades" and find the top 3 cities by completed trades using JOIN, GROUP BY, and ORDER BY.
7. Conditional logic with CASE: Special Bonus Logic
Pattern: CASE expressions with multiple conditions.
Interview goal: understand whether you can encode non-trivial business rules using boolean logic and CASE.
Practice on TheQueryLab: Solve "Special Bonus Logic" and calculate a bonus based on both ID parity and name prefix.
8. Classic join filter: Managers with at least 5 Direct Reports
Pattern: WHERE conditions + OR + NULL handling.
Interview goal: see whether you can keep a predicate readable while combining multiple cases (referred by someone else, or not referred at all).
Practice on TheQueryLab: Solve "Managers with at least 5 Direct Reports" and make sure you fully understand why referee_id <> 2 alone is not enough.
9. Third event per user: User's 3rd Transaction
Pattern: ROW_NUMBER() with PARTITION BY.
Interview goal: test if you can assign sequence numbers per user and select a specific rank (3rd transaction) while excluding users with too few rows.
Practice on TheQueryLab: Solve "User's 3rd Transaction" using a subquery/CTE and ROW_NUMBER() OVER (PARTITION BY user_id ORDER BY transaction_date).
10. Rolling averages: Tweets Rolling Average
Pattern: window frames with ROWS BETWEEN.
Interview goal: confirm you can reason about time windows and implement a 3-day rolling average in SQL.
Practice on TheQueryLab: Solve "Tweets Rolling Average" and implement a rolling window with ROWS BETWEEN 2 PRECEDING AND CURRENT ROW.
11. Window functions for retention: Active Users Over Time
Pattern: date grouping + window analytics.
Interview goal: see whether you can combine GROUP BY with window functions to track how metrics evolve.
Practice on TheQueryLab: Solve "User re-activation Month" and find the re-activated users month.
12. Joining multiple tables: Department Salary Reports
Pattern: multi-table joins + aggregation.
Interview goal: ensure you can correctly join employees, departments, and mapping tables and still aggregate accurately without double-counting.
Practice on TheQueryLab: Solve "Amazon Unpopular Books" and find the books that are un popular.
13. Subqueries vs JOINs: Filtering on Aggregates
Pattern: HAVING vs subquery in WHERE.
Interview goal: test whether you know when to use HAVING and when a subquery is clearer.
Practice on TheQueryLab: Solve "Customer Who Bought All Products" and report the customer IDs who bought all the products.
14. Distinct counts: Unique Users per City
Pattern: COUNT(DISTINCT ...).
Interview goal: validate that you understand distinct aggregations and their impact on performance.
Practice on TheQueryLab: Solve "Fraud IP Velocity" and identify any IP address that was used by more than 10 different Account IDs in a single day.
15. Date ranges and cohorts
Pattern: date filtering + bucketing.
Interview goal: see if you can turn wording like "last 30 days" or "between first and second purchase" into correct WHERE conditions and buckets.
Practice on TheQueryLab: Solve "Monthly Transaction" and monitor transaction health across different markets.
16. EXISTS vs JOIN: semi-joins
Pattern: rewriting "at least one matching row" as EXISTS.
Interview goal: check whether you can express membership-style questions using EXISTS instead of relying only on joins.
Practice on TheQueryLab: Solve "Winning Candidate" and find the name of the winning candidate.
17. Anti-joins: customers with no orders
Pattern: LEFT JOIN ... WHERE right.id IS NULL.
Interview goal: confirm you know the standard pattern to find entities with no matching child rows.
Practice on TheQueryLab: Solve "Customer Visit Without Transaction" and find who visited without making any transactions and the number of times they made these types of visits.
18. String operations and patterns
Pattern: LIKE, NOT LIKE, and basic string functions.
Interview goal: evaluate your ability to manipulate text fields (emails, names, URLs) in a clean and performant way.
Practice on TheQueryLab: Solve "Find Valid Emails" and identify all users who have a valid internal email address.
19. Ranking products by revenue
Pattern: combining aggregation with window ranking.
Interview goal: test if you can first aggregate revenue per product, then rank products globally or per category.
Practice on TheQueryLab: Solve "Remix Velocity" and rank sounds by their Remix Velocity.
20. Percentiles and top X%
Pattern: using window functions to approximate percentiles or top X%.
Interview goal: see if you can phrase questions like "top 10% of customers by spend" with NTILE() or similar techniques.
Practice on TheQueryLab: Solve "Health Care Patient Wait" and calculate the median wait time in minutes for each clinic.
Final tip: turn this into a 1–2 week practice plan
Instead of binge-reading solutions, schedule:
- Days 1–3: Questions 1–8 (filtering + basic aggregates)
- Days 4–7: Questions 9–15 (window functions + real business rules)
- Days 8–10: Questions 15–20 (advanced analytics + explanation skills)
By the end, you won't just recognize these 20 questions – you'll be able to derive the queries from scratch in an interview, using the same patterns you practiced on TheQueryLab.
