Level 2: Intermediate SQL Exercises
Level 2 is where SQL gets powerful: questions that span several tables. You build monthly revenue reports, attach customers to their orders, find people who never bought anything and rank your best customers.
These are also the queries that most often return wrong numbers without
any error: a LEFT JOIN quietly turned into an inner one, an order counted
twice after a join, NOT IN returning nothing because of one NULL. Each
lesson shows the trap, and the tests use data designed to catch it.
The 10 exercises in this level
- 1 SQL GROUP BY and HAVING Count and total each group Find the countries that bring in the most revenue: count and total orders with GROUP BY, and keep only the big groups with HAVING. Start here — no prerequisites
- 2 SQL Monthly Reports with GROUP BY Group by an expression Build a revenue report per month and sales channel: group by a calculated month and by a second column, without mixing up years. Recommended first: SQL GROUP BY and HAVING
- 3 SQL INNER JOIN Combine two tables on a key Put customer names next to their orders with JOIN ... ON, and see why a missing ON condition multiplies every row. Start here — no prerequisites
- 4 SQL LEFT JOIN Keep the rows with no match Count each customer's orders, including customers with zero, with LEFT JOIN, and avoid the WHERE that silently turns it back into an inner join. Recommended first: SQL INNER JOIN
- 5 SQL NOT EXISTS Find rows with no match Find the customers who never ordered with NOT EXISTS, and see why NOT IN returns nothing as soon as one NULL shows up. Recommended first: SQL LEFT JOIN
- 6 SQL Subqueries A query inside a query List the products priced above the average with a subquery, since an aggregate can't go straight into WHERE. Recommended first: SQL GROUP BY and HAVING
- 7 SQL Joins Across Three Tables Orders, items and products Total the revenue of each product category across orders, items and products, without counting an order once per item. Recommended first: SQL LEFT JOIN, SQL Monthly Reports with GROUP BY
- 8 SQL Self Join A table joined to itself Show every employee with their manager by joining the employees table to itself, without losing the boss who has no manager. Recommended first: SQL LEFT JOIN
- 9 SQL UNION and UNION ALL Stack two results Build one mailing list from customers and newsletter subscribers with UNION, and know when duplicates are removed or kept. Recommended first: SQL Self Join
- 10 Project: SQL Customer Leaderboard Everything from this level Rank the top 3 customers by paid revenue: join, filter, group, keep only regular customers with HAVING, then sort and limit. Recommended first: SQL Joins Across Three Tables, SQL Subqueries, SQL NOT EXISTS
Frequently asked questions
- What is the difference between WHERE and HAVING?
- `WHERE` filters rows before they are grouped; `HAVING` filters the groups after counting. A condition on `COUNT()` or `SUM()` belongs in `HAVING`.
- INNER JOIN or LEFT JOIN?
- An inner join keeps only the rows that match in both tables. A LEFT JOIN keeps every row of the left table and fills in NULL where nothing matched, which is what you need for "including customers with no orders".
- Why are my totals too big after a join?
- Joining one order to its 3 items repeats the order 3 times, so summing the order's total counts it 3 times. Sum the item amounts instead, or aggregate before joining. The three-table join exercise shows it.
- Should I use NOT IN or NOT EXISTS?
- Prefer `NOT EXISTS`. If the subquery of a `NOT IN` returns a single NULL, the whole condition is unknown and you get no rows at all.