SQL LEFT JOIN practice: 6 exercises with answers
Updated · Tested on PostgreSQL 18.6 and SQLite 3.54.1 (results generated , re-checked on every release)
Six exercises on LEFT JOIN: keeping customers who never ordered, finding them with IS NULL, counting with zeros, a self join that keeps the owner, and two conversion counts on the Streamly app.
Write each query, press Check answer and get told right away whether your result matches. Stuck? Take a hint, or open the answer. New to the topic? Read the SQL JOINs explained guide first.
The tables
- customers (id, name, city, joined_on)
- menu_items (id, name, category, price, vegetarian)
- orders (id, customer_id, ordered_on, status)
- order_items (order_id, item_id, quantity)
- staff (id, name, role, city, manager_id, hired_on)
- users (id, signed_up_on, country, source)
- plays (id, user_id, played_on, minutes, device)
- subscriptions (user_id, started_on, plan, cancelled_on)
- 1
List every customer's name with the id of each order they placed. Include customers who never ordered (their order id will be empty).
From the lesson LEFT JOIN · Rocket Pizza
Write your query here…Show the answerHide the answer
Answer (PostgreSQL and SQLite)
SELECT c.name, o.id FROM customers c LEFT JOIN orders o ON o.customer_id = c.id;Expected result (SQLite): first 8 of 19 rows
name id Maya Ortiz 1 Maya Ortiz 3 Maya Ortiz 9 Maya Ortiz 16 Jonah Reed 2 Jonah Reed 7 Jonah Reed 13 Priya Nair 4 - 2
Which customers have never placed an order? Show their names.
From the lesson LEFT JOIN · Rocket Pizza
Write your query here…Show the answerHide the answer
Answer (PostgreSQL and SQLite)
SELECT c.name FROM customers c LEFT JOIN orders o ON o.customer_id = c.id WHERE o.id IS NULL;Expected result (SQLite): 2 rows
name Nora Lindqvist Diego Santos - 3
Count each customer's orders, including zero for customers who never ordered. Show name and order_count, grouping by the customer's id and name.
From the lesson LEFT JOIN · Rocket Pizza
Write your query here…Show the answerHide the answer
Answer (PostgreSQL and SQLite)
SELECT c.name, COUNT(o.id) AS order_count FROM customers c LEFT JOIN orders o ON o.customer_id = c.id GROUP BY c.id, c.name;Expected result (SQLite): first 8 of 10 rows
name order_count Maya Ortiz 4 Jonah Reed 3 Priya Nair 1 Tomás Herrera 2 Ellie Park 2 Sam Okafor 2 Lena Fischer 1 Marcus Bell 2 - 4
List every staff member's name with their manager's name as manager, and this time include the owner with an empty manager.
From the lesson Self-joins · Rocket Pizza
Write your query here…Show the answerHide the answer
Answer (PostgreSQL and SQLite)
SELECT s.name, m.name AS manager FROM staff s LEFT JOIN staff m ON m.id = s.manager_id;Expected result (both engines): 8 rows
name manager Rosa Vega NULL Omar Haddad Rosa Vega Lia Moreau Rosa Vega Ben Carter Omar Haddad Yuki Tanaka Omar Haddad Ivan Petrov Lia Moreau Zara Ali Lia Moreau Noah Kim Lia Moreau - 5
For each signup country, show users (everyone who signed up) and subscribers (those with a subscription).
From the lesson Funnels and conversion · Streamly
Write your query here…Show the answerHide the answer
Answer (PostgreSQL and SQLite)
SELECT u.country, COUNT(*) AS users, COUNT(s.user_id) AS subscribers FROM users u LEFT JOIN subscriptions s ON s.user_id = u.id GROUP BY u.country;Expected result (SQLite): 5 rows
country users subscribers BR 46 15 DE 32 14 GB 45 14 IN 52 24 US 125 33 - 6
Conversion by signup source: users, subscribers and conversion_pct (subscribers ÷ users × 100, rounded to 1 decimal).
From the lesson Funnels and conversion · Streamly
Write your query here…Show the answerHide the answer
Answer (PostgreSQL and SQLite)
SELECT u.source, COUNT(*) AS users, COUNT(s.user_id) AS subscribers, ROUND(100.0 * COUNT(s.user_id) / COUNT(*), 1) AS conversion_pct FROM users u LEFT JOIN subscriptions s ON s.user_id = u.id GROUP BY u.source;Expected result (SQLite): 4 rows
source users subscribers conversion_pct ad 57 18 31.6 friend 91 39 42.9 podcast 55 14 25.5 search 97 29 29.9
The lessons have more like these
Each lesson teaches the idea step by step first, then gives you exercises with instant feedback and a daily streak.
- LEFT JOIN5 more exercises · sqlstreak Pro
- Self-joins5 more exercises · sqlstreak Pro
- Funnels and conversion8 more exercises · sqlstreak Pro
More practice sets
Check your level
The free SQL test takes about 10 minutes and tells you which lesson to start with.