Skip to the content

Keys

SQL · Unit 7: Joining tables

You've spent six units reading ids by eye. "Customer 2" was Luke, "product 4" was the Cable, and every time it was you who went to the other table to check.

Today the warehouse has order 10 ready. Who does it go to, and what's in it? orders answers with numbers. Two other tables know who the 3 is and what the 7 is.

SELECT *
FROM orders
WHERE id = 10;

Returns

idcustomer_idproduct_idquantityplaced
103722026-01-15

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. Complete the code

    Order 10 says customer_id 3. Who is that, and which city is the package going to? Fill in the blank to look them up in customers.

  2. 2. Predict the output

    The row for order 10 says customer_id 3, product_id 7, and quantity 2. What does this query return?

  3. 3. Complete the code

    To find out whether a column repeats, you count its distinct values and compare that with the number of rows. orders has 12 rows. Fill in the blank with the column that never repeats.

  4. 4. Multiple choice

    orders has three columns that hold id numbers: id, customer_id, and product_id. What is each one?

  5. 5. Put the lines in order

    Dylan is customer 6. Which orders has he placed, and on which days? Put in order the query that shows Dylan's orders, starting with the most recent. Two lines don't belong.

  6. 6. Predict the output

    Customers 5 and 6 are Valerie and Dylan, with two orders each. Customers 7 and 8 are Hannah and Brian. What does this query return?

  7. 7. Multiple choice

    The Mouse, which is product 2, drops in price. It's been sold in two orders, 3 and 7. In how many rows of the database does the price have to change?

  8. 8. Multiple choice

    Each order points to its customer. Why not the other way around, with an order_id column in customers?

  9. 9. Write the code

    The Cable, which is product 4, is running late, and the store is going to let the people who ordered it know. Write a query with a single column, the customer_id of that product's orders, with no repeats and from lowest to highest.

  10. 10. Find the bug

    The biggest order the store has ever received is order 4, for 10 units. Its row in orders says customer_id 4 and product_id 4. Which product was it? The answer should be the Cable, but this query says Mason. Which line has the problem?

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 →