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.
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)
- 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 answerHide 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
id name 1 Maya Ortiz 2 Jonah Reed 3 Maya Ortiz 4 Priya Nair 5 Tomás Herrera 6 Ellie Park 7 Jonah Reed 8 Sam Okafor - 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 answerHide 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
id name status 4 Priya Nair cancelled 11 Tomás Herrera cancelled - 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 answerHide 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
name quantity Four Cheese Orbit 1 Garlic Knots 1 Cola 1 - 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 answerHide 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
name SUM(oi.quantity) BBQ Meteor 4 Caesar Salad 1 Cola 5 Four Cheese Orbit 5 Garlic Knots 4 Lemon Soda 4 Margherita 5 Pepperoni Rocket 7 - 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 answerHide 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
name revenue BBQ Meteor 62 Caesar Salad 7 Cola 12.5 Four Cheese Orbit 70 Garlic Knots 20 Lemon Soda 12 Margherita 55 Pepperoni Rocket 94.5 - 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 answerHide 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
id total 10 59 6 42 13 40.5 12 32.5 1 28 8 24 2 22 3 21.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.
- Your first JOIN5 more exercises · sqlstreak Pro
- Joins that add up6 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.