JOIN
SQL · Unit 7: Joining tables
- Full plan
- SQL
- 10 exercises
Who placed each order? The owner wants the list with names, and by hand that's twelve trips to customers, one per order.
JOIN makes that trip for you. It puts each order next to its customer's row. ON says which row that is: the customer whose id equals the order's customer_id. Since both tables have an id column, you write the table, a dot, and the column. customers.id reads "the id of customers."
SELECT placed, name
FROM orders
JOIN customers
ON orders.customer_id = customers.id;Returns
| placed | name |
|---|---|
| 2026-01-03 | Ann |
| 2026-01-04 | Ann |
| 2026-01-04 | Luke |
| 2026-01-09 | Mason |
| 2026-01-10 | Valerie |
| 2026-01-11 | Luke |
| 2026-01-12 | Ann |
| 2026-01-12 | Valerie |
| 2026-01-14 | Dylan |
| 2026-01-15 | Chloë |
| 2026-01-15 | Dylan |
| 2026-01-20 | Luke |
The rest of the explanation is in the lesson, which is part of the full plan.
Exercises in this lesson
You do them in the app, which checks them on the spot and explains why.
1. Multiple choice
That list has 12 rows today. Tomorrow Brian, who is in
customersand has never bought anything, places his first order. How many rows will it have then?2. Complete the code
Who bought in the first few days, before January 5? Fill in the blanks with the two words that join the tables.
3. Predict the output
Valerie is customer 5. She has placed two orders, one on January 10 and another on the 12th. What does this query return?
4. Complete the code
There were two orders on January 4. Which product was each one, and how many units? Fill in the missing side of the
ON.5. Predict the output
Orders of 3 units or more ship in a big box, and there are three of them. Order 3, from customer 2, has 5 units of product 2. Order 4, from customer 4, has 10 of product 4. Order 8, from customer 5, has 3 of product 4. In
products, 2 is the Mouse, 4 is the Cable, and 5 is the Laptop. What does this query return?6. Put the lines in order
Three orders were for 2 units. Whose is each one, and which city is it going to? Put in order the query that shows the date, the name, and the city, with the most recent on top. Two lines don't belong.
7. Multiple choice
Someone changes
JOIN customerstoINNER JOIN customersin the list of orders with names, which gives 12 rows. What happens to the result?8. Find the bug
This query should list the big-box orders, the ones of 3 units or more, with who placed each one. It runs without an error, and it says the one from January 12 is Brian's. Brian has never ordered anything. Which line has the problem?
9. Put the lines in order
What has been ordered from January 14 on? Put in order the query that shows the date, the product, and the quantity, by date and, within each day, with the smallest order first. Two lines don't belong.
10. Write the code
Today is January 15, 2026, and
ordershas one dated later. Whose is it? Write a query with two columns, the order'splaceddate and then thenameof whoever placed it, for the orders dated after today.
It opens in your browser. This lesson is part of the full plan; the first unit of each course is free.