Interview preparation
SQL practice questions: in-depth answers
Solve 108 free problems on LeetCode and DataLemur, then check 29 worked answers that explain the patterns. Tick each one off as you go.
108 free SQL practice problems on LeetCode and DataLemur
Write and run real queries against the platform's test cases. These are the free LeetCode SQL 50 and the DataLemur questions anyone can solve without paying. Each link opens the platform in a new tab; tick a problem off here when you solve it. Stuck? The worked answers further down explain the patterns.
108 problems in 10 topics, from basic to advanced. 0 of 108 done.
LeetCode SQL 50: Select and filter0/5
- 1757. Recyclable and Low Fat Products (opens LeetCode in a new tab)EasyLeetCode (opens in a new tab)
- 584. Find Customer Referee (opens LeetCode in a new tab)EasyLeetCode (opens in a new tab)
- 595. Big Countries (opens LeetCode in a new tab)EasyLeetCode (opens in a new tab)
- 1148. Article Views I (opens LeetCode in a new tab)EasyLeetCode (opens in a new tab)
- 1683. Invalid Tweets (opens LeetCode in a new tab)EasyLeetCode (opens in a new tab)
LeetCode SQL 50: Joins0/10
- 175. Combine Two Tables (opens LeetCode in a new tab)EasyLeetCode (opens in a new tab)
- 1378. Replace Employee ID With The Unique Identifier (opens LeetCode in a new tab)EasyLeetCode (opens in a new tab)
- 1068. Product Sales Analysis I (opens LeetCode in a new tab)EasyLeetCode (opens in a new tab)
- 1581. Customer Who Visited but Did Not Make Any Transactions (opens LeetCode in a new tab)EasyLeetCode (opens in a new tab)
- 197. Rising Temperature (opens LeetCode in a new tab)EasyLeetCode (opens in a new tab)
- 1661. Average Time of Process per Machine (opens LeetCode in a new tab)EasyLeetCode (opens in a new tab)
- 577. Employee Bonus (opens LeetCode in a new tab)EasyLeetCode (opens in a new tab)
- 1280. Students and Examinations (opens LeetCode in a new tab)EasyLeetCode (opens in a new tab)
- 570. Managers with at Least 5 Direct Reports (opens LeetCode in a new tab)MediumLeetCode (opens in a new tab)
- 1934. Confirmation Rate (opens LeetCode in a new tab)MediumLeetCode (opens in a new tab)
LeetCode SQL 50: Aggregate functions0/8
- 620. Not Boring Movies (opens LeetCode in a new tab)EasyLeetCode (opens in a new tab)
- 1251. Average Selling Price (opens LeetCode in a new tab)EasyLeetCode (opens in a new tab)
- 1075. Project Employees I (opens LeetCode in a new tab)EasyLeetCode (opens in a new tab)
- 1633. Percentage of Users Attended a Contest (opens LeetCode in a new tab)EasyLeetCode (opens in a new tab)
- 1211. Queries Quality and Percentage (opens LeetCode in a new tab)EasyLeetCode (opens in a new tab)
- 1193. Monthly Transactions I (opens LeetCode in a new tab)MediumLeetCode (opens in a new tab)
- 1174. Immediate Food Delivery II (opens LeetCode in a new tab)MediumLeetCode (opens in a new tab)
- 550. Game Play Analysis IV (opens LeetCode in a new tab)MediumLeetCode (opens in a new tab)
LeetCode SQL 50: Sorting and grouping0/9
- 2356. Number of Unique Subjects Taught by Each Teacher (opens LeetCode in a new tab)EasyLeetCode (opens in a new tab)
- 1141. User Activity for the Past 30 Days I (opens LeetCode in a new tab)EasyLeetCode (opens in a new tab)
- 596. Classes More Than 5 Students (opens LeetCode in a new tab)EasyLeetCode (opens in a new tab)
- 1729. Find Followers Count (opens LeetCode in a new tab)EasyLeetCode (opens in a new tab)
- 619. Biggest Single Number (opens LeetCode in a new tab)EasyLeetCode (opens in a new tab)
- 586. Customer Placing the Largest Number of Orders (opens LeetCode in a new tab)EasyLeetCode (opens in a new tab)
- 182. Duplicate Emails (opens LeetCode in a new tab)EasyLeetCode (opens in a new tab)
- 1070. Product Sales Analysis III (opens LeetCode in a new tab)MediumLeetCode (opens in a new tab)
- 1045. Customers Who Bought All Products (opens LeetCode in a new tab)MediumLeetCode (opens in a new tab)
LeetCode SQL 50: Advanced select and joins0/8
- 1731. The Number of Employees Which Report to Each Employee (opens LeetCode in a new tab)EasyLeetCode (opens in a new tab)
- 1789. Primary Department for Each Employee (opens LeetCode in a new tab)EasyLeetCode (opens in a new tab)
- 610. Triangle Judgment (opens LeetCode in a new tab)EasyLeetCode (opens in a new tab)
- 180. Consecutive Numbers (opens LeetCode in a new tab)MediumLeetCode (opens in a new tab)
- 1164. Product Price at a Given Date (opens LeetCode in a new tab)MediumLeetCode (opens in a new tab)
- 1204. Last Person to Fit in the Bus (opens LeetCode in a new tab)MediumLeetCode (opens in a new tab)
- 1907. Count Salary Categories (opens LeetCode in a new tab)MediumLeetCode (opens in a new tab)
- 262. Trips and Users (opens LeetCode in a new tab)HardLeetCode (opens in a new tab)
LeetCode SQL 50: Subqueries0/9
- 1978. Employees Whose Manager Left the Company (opens LeetCode in a new tab)EasyLeetCode (opens in a new tab)
- 1084. Sales Analysis III (opens LeetCode in a new tab)EasyLeetCode (opens in a new tab)
- 626. Exchange Seats (opens LeetCode in a new tab)MediumLeetCode (opens in a new tab)
- 1341. Movie Rating (opens LeetCode in a new tab)MediumLeetCode (opens in a new tab)
- 1321. Restaurant Growth (opens LeetCode in a new tab)MediumLeetCode (opens in a new tab)
- 602. Friend Requests II: Who Has the Most Friends (opens LeetCode in a new tab)MediumLeetCode (opens in a new tab)
- 585. Investments in 2016 (opens LeetCode in a new tab)MediumLeetCode (opens in a new tab)
- 184. Department Highest Salary (opens LeetCode in a new tab)MediumLeetCode (opens in a new tab)
- 1158. Market Analysis I (opens LeetCode in a new tab)MediumLeetCode (opens in a new tab)
LeetCode SQL 50: Strings, dates and regex0/5
- 1667. Fix Names in a Table (opens LeetCode in a new tab)EasyLeetCode (opens in a new tab)
- 1527. Patients With a Condition (opens LeetCode in a new tab)EasyLeetCode (opens in a new tab)
- 1484. Group Sold Products By The Date (opens LeetCode in a new tab)EasyLeetCode (opens in a new tab)
- 1327. List the Products Ordered in a Period (opens LeetCode in a new tab)EasyLeetCode (opens in a new tab)
- 1517. Find Users With Valid E-Mails (opens LeetCode in a new tab)EasyLeetCode (opens in a new tab)
DataLemur: easy interview questions0/21
- Histogram of Tweets (opens DataLemur in a new tab)EasyDataLemur (opens in a new tab)
- Data Science Skills (opens DataLemur in a new tab)EasyDataLemur (opens in a new tab)
- Page With No Likes (opens DataLemur in a new tab)EasyDataLemur (opens in a new tab)
- Unfinished Parts (opens DataLemur in a new tab)EasyDataLemur (opens in a new tab)
- Laptop vs. Mobile Viewership (opens DataLemur in a new tab)EasyDataLemur (opens in a new tab)
- Average Post Hiatus (Part 1) (opens DataLemur in a new tab)EasyDataLemur (opens in a new tab)
- Teams Power Users (opens DataLemur in a new tab)EasyDataLemur (opens in a new tab)
- Duplicate Job Listings (opens DataLemur in a new tab)EasyDataLemur (opens in a new tab)
- Cities With Completed Trades (opens DataLemur in a new tab)EasyDataLemur (opens in a new tab)
- Average Review Ratings (opens DataLemur in a new tab)EasyDataLemur (opens in a new tab)
- Well Paid Employees (opens DataLemur in a new tab)EasyDataLemur (opens in a new tab)
- Final Account Balance (opens DataLemur in a new tab)EasyDataLemur (opens in a new tab)
- App Click-through Rate (CTR) (opens DataLemur in a new tab)EasyDataLemur (opens in a new tab)
- Second Day Confirmation (opens DataLemur in a new tab)EasyDataLemur (opens in a new tab)
- IBM db2 Product Analytics (opens DataLemur in a new tab)EasyDataLemur (opens in a new tab)
- Cards Issued Difference (opens DataLemur in a new tab)EasyDataLemur (opens in a new tab)
- Compressed Mean (opens DataLemur in a new tab)EasyDataLemur (opens in a new tab)
- Pharmacy Analytics (Part 1) (opens DataLemur in a new tab)EasyDataLemur (opens in a new tab)
- Pharmacy Analytics (Part 2) (opens DataLemur in a new tab)EasyDataLemur (opens in a new tab)
- Pharmacy Analytics (Part 3) (opens DataLemur in a new tab)EasyDataLemur (opens in a new tab)
- Patient Support Analysis (Part 1) (opens DataLemur in a new tab)EasyDataLemur (opens in a new tab)
DataLemur: medium interview questions0/19
- User's Third Transaction (opens DataLemur in a new tab)MediumDataLemur (opens in a new tab)
- Second Highest Salary (opens DataLemur in a new tab)MediumDataLemur (opens in a new tab)
- Sending vs. Opening Snaps (opens DataLemur in a new tab)MediumDataLemur (opens in a new tab)
- Tweets' Rolling Averages (opens DataLemur in a new tab)MediumDataLemur (opens in a new tab)
- Highest-Grossing Items (opens DataLemur in a new tab)MediumDataLemur (opens in a new tab)
- Top Three Salaries (opens DataLemur in a new tab)MediumDataLemur (opens in a new tab)
- Signup Activation Rate (opens DataLemur in a new tab)MediumDataLemur (opens in a new tab)
- Spotify Streaming History (opens DataLemur in a new tab)MediumDataLemur (opens in a new tab)
- Supercloud Customer (opens DataLemur in a new tab)MediumDataLemur (opens in a new tab)
- Odd and Even Measurements (opens DataLemur in a new tab)MediumDataLemur (opens in a new tab)
- Swapped Food Delivery (opens DataLemur in a new tab)MediumDataLemur (opens in a new tab)
- FAANG Stock Min-Max (Part 1) (opens DataLemur in a new tab)MediumDataLemur (opens in a new tab)
- Best-Selling Product (opens DataLemur in a new tab)MediumDataLemur (opens in a new tab)
- User Shopping Sprees (opens DataLemur in a new tab)MediumDataLemur (opens in a new tab)
- Histogram of Users and Purchases (opens DataLemur in a new tab)MediumDataLemur (opens in a new tab)
- Compressed Mode (opens DataLemur in a new tab)MediumDataLemur (opens in a new tab)
- Card Launch Success (opens DataLemur in a new tab)MediumDataLemur (opens in a new tab)
- International Call Percentage (opens DataLemur in a new tab)MediumDataLemur (opens in a new tab)
- Patient Support Analysis (Part 2) (opens DataLemur in a new tab)MediumDataLemur (opens in a new tab)
DataLemur: hard interview questions0/14
- Active User Retention (opens DataLemur in a new tab)HardDataLemur (opens in a new tab)
- Y-on-Y Growth Rate (opens DataLemur in a new tab)HardDataLemur (opens in a new tab)
- Maximize Prime Item Inventory (opens DataLemur in a new tab)HardDataLemur (opens in a new tab)
- Median Google Search Frequency (opens DataLemur in a new tab)HardDataLemur (opens in a new tab)
- Advertiser Status (opens DataLemur in a new tab)HardDataLemur (opens in a new tab)
- Consecutive Filing Years (opens DataLemur in a new tab)HardDataLemur (opens in a new tab)
- Marketing Touch Streak (opens DataLemur in a new tab)HardDataLemur (opens in a new tab)
- 3-Topping Pizzas (opens DataLemur in a new tab)HardDataLemur (opens in a new tab)
- Patient Support Analysis (Part 3) (opens DataLemur in a new tab)HardDataLemur (opens in a new tab)
- Patient Support Analysis (Part 4) (opens DataLemur in a new tab)HardDataLemur (opens in a new tab)
- Repeated Payments (opens DataLemur in a new tab)HardDataLemur (opens in a new tab)
- Reactivated Users (opens DataLemur in a new tab)HardDataLemur (opens in a new tab)
- Senior Managers (opens DataLemur in a new tab)HardDataLemur (opens in a new tab)
- Server Utilization Time (opens DataLemur in a new tab)HardDataLemur (opens in a new tab)
Ticks are saved in this browser only and match the ticks on the interview pages.
Worked SQL problems with answers
Write the query yourself first, then compare it with the worked answer, sample data and output.
Showing 8 of 8 questions · 0 done
| # | Question | Difficulty | Type | Practise | Done |
|---|---|---|---|---|---|
| 1 | Find customers who never placed an order, and explain NOT IN vs NOT EXISTS. | Easy | coding, debugging | LeetCode (opens in a new tab) | |
| 2 | Calculate month-over-month and year-over-year revenue growth in SQL. | Medium | coding | — | |
| 3 | Compute a running total and a 3-day moving average of daily revenue in SQL. | Medium | coding | — | |
| 4 | Find the second-highest salary without using a simple MAX approach. | Medium | coding | LeetCode (opens in a new tab) | |
| 5 | How would you detect and remove duplicate records safely? | Medium | coding, scenario | LeetCode (opens in a new tab) | |
| 6 | Return the top three salaries in each department, including ties. | Medium | coding | LeetCode (opens in a new tab) | |
| 7 | Revenue doubled after you joined a second table. What happened and how do you fix it? | Medium | debugging, coding | — | |
| 8 | Write an SCD Type 2 upsert in SQL that closes old versions and inserts new ones. | Hard | coding, scenario | — |
Ticks are saved in this browser only. No account needed.
No questions match these filters
Clear a filter to see more questions.
Business case studies
Real business questions (revenue, churn, funnels, retention) solved step by step, several ways.
Showing 16 of 16 questions · 0 done
Ticks are saved in this browser only. No account needed.
No questions match these filters
Clear a filter to see more questions.
Concept and troubleshooting questions
How SQL runs, how joins and window functions differ, and how to find and fix a slow query.
Showing 5 of 5 questions · 0 done
| # | Question | Difficulty | Type | Done |
|---|---|---|---|---|
| 1 | Explain INNER JOIN vs LEFT JOIN with a practical example. | Easy | conceptual, coding | |
| 2 | How do window functions differ from GROUP BY? | Easy | conceptual, coding | |
| 3 | In what order is a SQL query evaluated, and what is the difference between WHERE and HAVING? | Easy | conceptual, debugging | |
| 4 | There is an index on the column, but EXPLAIN shows a sequential scan. Why? | Medium | optimization, debugging | |
| 5 | How would you optimize a slow analytical SQL query? | Hard | optimization, scenario |
Ticks are saved in this browser only. No account needed.
No questions match these filters
Clear a filter to see more questions.
How to use this page
This page collects 29 SQL interview questions answered in depth (4 easy, 16 medium and 9 hard), ordered from the basics to the harder design and troubleshooting questions. They include “Explain INNER JOIN vs LEFT JOIN with a practical example”, “Find customers who never placed an order, and explain NOT IN vs NOT EXISTS”, “How do window functions differ from GROUP BY” and “In what order is a SQL query evaluated, and what is the difference between WHERE and HAVING”.
Each answer opens with a short version you could say out loud in an interview, then explains why it works, shows a worked example, and lists the mistakes and follow-up questions interviewers use. For breadth first, read Top 50 SQL Interview Questions and Answers for Data Engineers, then come back here to go deep on the questions you are most likely to be asked. If a topic is new to you, learn it in the SQL course first. Tick each question off as you practise; your progress is saved in this browser.
Question bank
Start with the question bank · 50 questions with answersTop 50 SQL Interview Questions and Answers for Data EngineersConcept review
- LessonBeginnerSQL Aggregations: GROUP BY, HAVING and Conditional Aggregation
- LessonIntermediateCAP Theorem, PACELC and BASE Explained for Data Engineers
- LessonIntermediateSQL Subqueries and CTEs: Derived Tables, Correlated Queries and Temporary Tables
- LessonIntermediateTime-Series SQL: Date Buckets, Calendars, YoY, MoM and Rolling Metrics
- LessonBeginnerSQL Functions: Strings, Dates, Type Casting and Arithmetic
- LessonAdvancedGaps and Islands, Streaks and Sessionization in SQL
- LessonAdvancedSQL Indexes: B-tree, Composite, Covering and Bitmap Indexes
- LessonBeginnerSQL Joins: INNER, LEFT, RIGHT, FULL, Self and Cross Joins
- LessonAdvancedSemi-Structured SQL: JSON, Arrays and Regular Expressions
- LessonBeginnerSQL Operators, NULL Handling and CASE Expressions
- LessonAdvancedPartitioning, Materialized Views, Columnar Storage and Sharding
- LessonIntermediatePIVOT, UNPIVOT, GROUPING SETS, ROLLUP and CUBE
- LessonAdvancedSQL Query Optimization and Execution Plans
- LessonAdvancedInside the Query Optimizer: Join Algorithms, Statistics and Memory
- LessonAdvancedRecursive CTEs: Hierarchies and Graph Traversal in SQL
- LessonBeginnerWhat Is a Relational Database (RDBMS)? Tables, Keys and SQL Explained
- LessonAdvancedProduct Analytics SQL: Retention, Cohorts, Funnels and Attribution
- LessonIntermediateSemi-Joins, Anti-Joins and LATERAL Joins in SQL
- LessonBeginnerSQL Set Operations: UNION, UNION ALL, INTERSECT and EXCEPT
- LessonBeginnerSQL Fundamentals: SELECT, WHERE, DISTINCT, ORDER BY and LIMIT
- LessonIntermediateTop 50 SQL Interview Questions and Answers for Data Engineers
- LessonAdvancedSQL at Scale: Window Performance, Skew and Approximate Distinct
- LessonIntermediateTop-N per Group, Deduplication and SCD Queries
- LessonAdvancedTransactions, Isolation Levels, Locking and MVCC
- LessonIntermediateWindow Frames: Running Totals, Moving Averages and Percentiles
- LessonIntermediateSQL Window Functions: Ranking, LAG/LEAD and FIRST_VALUE

