Skip to the content

JOIN

SQL · Unit 7: Joining tables

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

placedname
2026-01-03Ann
2026-01-04Ann
2026-01-04Luke
2026-01-09Mason
2026-01-10Valerie
2026-01-11Luke
2026-01-12Ann
2026-01-12Valerie
2026-01-14Dylan
2026-01-15Chloë
2026-01-15Dylan
2026-01-20Luke

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. 1. Multiple choice

    That list has 12 rows today. Tomorrow Brian, who is in customers and has never bought anything, places his first order. How many rows will it have then?

  2. 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. 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. 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. 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. 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. 7. Multiple choice

    Someone changes JOIN customers to INNER JOIN customers in the list of orders with names, which gives 12 rows. What happens to the result?

  8. 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. 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. 10. Write the code

    Today is January 15, 2026, and orders has one dated later. Whose is it? Write a query with two columns, the order's placed date and then the name of whoever placed it, for the orders dated after today.

Do this lesson

It opens in your browser. This lesson is part of the full plan; the first unit of each course is free.

See all lessons →