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.

Answers are checked on the engine you pick.
The tables

Rocket Pizza

  • 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)

Streamly

  • users (id, signed_up_on, country, source)
  • plays (id, user_id, played_on, minutes, device)
  • subscriptions (user_id, started_on, plan, cancelled_on)
  1. 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 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

    nameid
    Maya Ortiz1
    Maya Ortiz3
    Maya Ortiz9
    Maya Ortiz16
    Jonah Reed2
    Jonah Reed7
    Jonah Reed13
    Priya Nair4
  2. 2

    Which customers have never placed an order? Show their names.

    From the lesson LEFT JOIN · Rocket Pizza

    Write your query here…
    Show 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. 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 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

    nameorder_count
    Maya Ortiz4
    Jonah Reed3
    Priya Nair1
    Tomás Herrera2
    Ellie Park2
    Sam Okafor2
    Lena Fischer1
    Marcus Bell2
  4. 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 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

    namemanager
    Rosa VegaNULL
    Omar HaddadRosa Vega
    Lia MoreauRosa Vega
    Ben CarterOmar Haddad
    Yuki TanakaOmar Haddad
    Ivan PetrovLia Moreau
    Zara AliLia Moreau
    Noah KimLia Moreau
  5. 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 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

    countryuserssubscribers
    BR4615
    DE3214
    GB4514
    IN5224
    US12533
  6. 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 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

    sourceuserssubscribersconversion_pct
    ad571831.6
    friend913942.9
    podcast551425.5
    search972929.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.

More practice sets

Check your level

The free SQL test takes about 10 minutes and tells you which lesson to start with.