SQL joins lesson with the shop database

A 90-minute lesson that moves from one table to four, with every query run in the SQL sandbox and its tasks checked automatically. Step-by-step notes, differentiation, homework and solutions included.

Lesson plan1 h 30 minBachelor +2CC BY 4.0

What you will learn

  • Explain primary and foreign keys
  • Write INNER and LEFT joins
  • Combine joins with GROUP BY to answer business questions
  • Explain what a join without an ON condition returns

Before you start: SELECT, WHERE and ORDER BY on a single table.

Everyone opens the SQL sandbox with the office supplies shop dataset.

Running the lesson

90 min
  1. 10 min

    Open the Tables panel. Which column of orders points at customers? Introduce primary and foreign keys.

  2. 15 min

    Shop task 3, "Orders with names": the first join, written together.

  3. 15 min

    LEFT JOIN: who has never ordered? Shop task 5 in pairs.

  4. 20 min

    Shop tasks 4 and 6: joins with GROUP BY. Learners check their own answers.

  5. 20 min

    The "Joining tables in SQL" exercise, with a class link if you want the results.

  6. 10 min

    Wrap up: what happens without ON? Run it and count the rows.

Materials

  • One device with a current browser per learner or pair. The sandbox runs SQLite inside the page: no installation, no account, and nothing is sent to a server.

  • A projector for your own sandbox.

  • The Joining tables in SQL exercise. Unfold "Use it with a class" on its page and copy the class link before the lesson.

  • Optional: the shop database as an SQL script, with a data dictionary to print, if learners should also load it into a database on their own computers.

Step by step

1. Keys (10 minutes). In the Tables panel, open customers and orders. The column marked "key" in each table is its primary key: a number that identifies one row. Ask: which column of orders says who placed the order? (customer_id.) That is a foreign key: it points at the primary key of another table. Draw the four tables on the board with arrows from each foreign key to its key.

2. The first join (15 minutes). Pick shop task 3, "Orders with names". Write it together, one clause at a time:

Orders with names
SELECT orders.id, customers.name, orders.order_date
FROM orders
JOIN customers ON customers.id = orders.customer_id;

Learners press "Check my query". Point out the table names in front of each column: both tables have a column called id, so SQL needs to know which one you mean.

3. LEFT JOIN (15 minutes). Ask: "Which customer has never ordered?" Pairs try shop task 5. Let them try an inner join first and notice that the customer they are looking for cannot appear in it. Then show LEFT JOIN with WHERE orders.id IS NULL. The answer is Hannah Wolf.

4. Joins with GROUP BY (20 minutes). Shop task 4 (value of each order) joins order_items to products and adds up quantity × price per order. Shop task 6 (units by category) needs three tables. Learners work in pairs and check their answers themselves; use Hint before Solution.

5. The exercise (20 minutes). Learners open your class link and do the exercise alone. Paste the result codes into Class results at the end or before the next lesson.

6. Wrap-up (10 minutes). Remove the ON line from the query in step 2 and run it. It returns 80 rows: every order next to every customer (10 × 8). Ask why, and why the ON condition matters. Finish with the query below.

A query to show at the end

Every customer with their number of orders
SELECT customers.name, COUNT(orders.id) AS orders
FROM customers
LEFT JOIN orders ON orders.customer_id = customers.id
GROUP BY customers.name
ORDER BY orders DESC;

Hannah Wolf shows 0: COUNT(orders.id) counts only rows where an order exists, while COUNT(*) would count her joined row as 1.

Solutions to the shop tasks

Task 3, Orders with names: 10 rows, one per order. Anna Berger appears three times (orders 1, 3 and 10).

Task 4, Value of each order: order 2 is the largest with 298.00, then order 6 (227.00) and order 3 (163.00); order 4 is the smallest with 14.00.

Task 4
SELECT order_items.order_id, ROUND(SUM(order_items.quantity * products.price), 2) AS total
FROM order_items
JOIN products ON products.id = order_items.product_id
GROUP BY order_items.order_id
ORDER BY total DESC;

Task 5, Customers without orders: Hannah Wolf.

Task 6, Units by category in shipped orders: Paper 42, Writing 18, Furniture 6.

Task 6
SELECT products.category, SUM(order_items.quantity) AS units
FROM order_items
JOIN products ON products.id = order_items.product_id
JOIN orders ON orders.id = order_items.order_id
WHERE orders.status = 'shipped'
GROUP BY products.category;

Things to know about the automatic check

  • "Check my query" compares the learner's result with the expected result: the same number of columns and rows, and the same values. Column names do not count, so AS total or no alias are both right.

  • Columns are compared in order. In task 3 the expected order is id, name, date. A learner who writes id, date, name sees "some values differ"; let them compare with the column order of the solution.

  • Row order only counts where the task asks for a sort (task 4 here).

  • "Reset data" restores the starting tables if a learner changed them.

Typical misconceptions

  • "A join merges two tables into one forever." A join only builds a result for this query; the tables stay as they are.

  • "LEFT means the table on the left side of the screen." It means the table named before LEFT JOIN.

  • *"COUNT() counts orders."** After a LEFT JOIN it counts rows, including the empty one for a customer without orders.

Differentiation

  • Easier: learners work in pairs and may use Hint at any time. Skip task 6 and do task 4 together.

  • Harder: which customers bought furniture? (Four tables, SELECT DISTINCT.) Then: how much did each customer spend on shipped orders?

  • Fast finishers: design a fifth table, suppliers, and say which existing table would need a new foreign key.

Homework

  1. Write a query that lists every product with the total number of units sold, including products that were never sold. Which join do you need, and why?

  2. Explain in three sentences, in your own words, the difference between JOIN and LEFT JOIN, using Hannah Wolf as the example.

Homework and extension solutions

1. A LEFT JOIN from products, so products without order items stay in the result:

Units sold per product
SELECT products.name, SUM(order_items.quantity) AS units
FROM products
LEFT JOIN order_items ON order_items.product_id = products.id
GROUP BY products.id;

In this data every product was sold at least once: Printer paper 30, Notebook A5 26, Ballpoint pens 13, Whiteboard markers 5, Desk lamp 4, Office chair 3.

2. A good answer: JOIN shows only customers who have at least one matching order, so Hannah Wolf is missing. LEFT JOIN keeps every customer from the first table and fills the order columns with NULL when there is no match, so Hannah appears once with an empty order.

Furniture buyers: Ben Okafor, Anna Berger and Farid Haddad. Spending on shipped orders: Ben Okafor 343.00, Farid Haddad 227.00, Anna Berger 222.50, Emma Schulz 42.00, David Lee 33.20.

Reuse this material

CC BY 4.0

You may copy, adapt and share it, also commercially, as long as you credit ManyPI and link back.

"SQL joins lesson with the shop database" by ManyPI (https://manypi.com/edu/sql-joins-lesson), licensed under CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/)