SQL 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 combining tables with INNER JOIN: orders with their customers, order lines with menu items, then joins with GROUP BY for quantities, revenue and order totals. They use Rocket Pizza.

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)
  1. 1

    List every order id with the name of the customer who placed it.

    From the lesson Your first JOIN · Rocket Pizza

    Write your query here…
    Show the answer

    Answer (PostgreSQL and SQLite)

    SELECT o.id, c.name FROM orders o JOIN customers c ON c.id = o.customer_id;

    Expected result (both engines): first 8 of 17 rows

    idname
    1Maya Ortiz
    2Jonah Reed
    3Maya Ortiz
    4Priya Nair
    5Tomás Herrera
    6Ellie Park
    7Jonah Reed
    8Sam Okafor
  2. 2

    Show the order id, customer name and status of every cancelled order.

    From the lesson Your first JOIN · Rocket Pizza

    Write your query here…
    Show the answer

    Answer (PostgreSQL and SQLite)

    SELECT o.id, c.name, o.status FROM orders o JOIN customers c ON c.id = o.customer_id WHERE o.status = 'cancelled';

    Expected result (both engines): 2 rows

    idnamestatus
    4Priya Naircancelled
    11Tomás Herreracancelled
  3. 3

    What was in order 3? Join order_items to menu_items and show each item's name and its quantity.

    From the lesson Your first JOIN · Rocket Pizza

    Write your query here…
    Show the answer

    Answer (PostgreSQL and SQLite)

    SELECT m.name, oi.quantity FROM order_items oi JOIN menu_items m ON m.id = oi.item_id WHERE oi.order_id = 3;

    Expected result (both engines): 3 rows

    namequantity
    Four Cheese Orbit1
    Garlic Knots1
    Cola1
  4. 4

    How many of each menu item were sold in total? Show the item name and the total quantity, one row per item.

    From the lesson Joins that add up · Rocket Pizza

    Write your query here…
    Show the answer

    Answer (PostgreSQL and SQLite)

    SELECT m.name, SUM(oi.quantity) FROM order_items oi JOIN menu_items m ON m.id = oi.item_id GROUP BY m.name;

    Expected result (SQLite): first 8 of 11 rows

    nameSUM(oi.quantity)
    BBQ Meteor4
    Caesar Salad1
    Cola5
    Four Cheese Orbit5
    Garlic Knots4
    Lemon Soda4
    Margherita5
    Pepperoni Rocket7
  5. 5

    How much money did each menu item bring in? Show the item name and revenue: its quantity times its price, added up.

    From the lesson Joins that add up · Rocket Pizza

    Write your query here…
    Show the answer

    Answer (PostgreSQL and SQLite)

    SELECT m.name, SUM(oi.quantity * m.price) AS revenue FROM order_items oi JOIN menu_items m ON m.id = oi.item_id GROUP BY m.name;

    Expected result (SQLite): first 8 of 11 rows

    namerevenue
    BBQ Meteor62
    Caesar Salad7
    Cola12.5
    Four Cheese Orbit70
    Garlic Knots20
    Lemon Soda12
    Margherita55
    Pepperoni Rocket94.5
  6. 6

    Show each delivered order's id and total (quantity times price, added up), highest total first.

    From the lesson Joins that add up · Rocket Pizza

    Write your query here…
    Show the answer

    Answer (PostgreSQL and SQLite)

    SELECT o.id, SUM(oi.quantity * m.price) AS total FROM orders o JOIN order_items oi ON oi.order_id = o.id JOIN menu_items m ON m.id = oi.item_id WHERE o.status = 'delivered' GROUP BY o.id ORDER BY total DESC;

    Expected result (both engines): first 8 of 13 rows

    idtotal
    1059
    642
    1340.5
    1232.5
    128
    824
    222
    321.5

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.