Skip to the content

WHERE

SQL · Unit 2: Filtering

Last unit you spotted by eye that the Cable and the Speaker were at 0. With eight products you can. With eight thousand, you can't.

What did the store run out of? WHERE goes after the FROM and carries a condition. SQL tests it on every row and lets through only the ones where it's true.

SELECT name
FROM products
WHERE stock = 0;

Returns

name
Cable
Speaker

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. Predict the output

    Two people in this table are the same age. What does this query return?

  2. 2. Multiple choice

    The orders table has 12 rows and 5 columns. 6 of those orders are for a single unit. What shape does this query's result have?

  3. 3. Put the lines in order

    The owner is going to call the supplier and needs the name and the price of what ran out. Put the query in order. One line doesn't belong.

  4. 4. Predict the output

    A customer wants something cheaper than the Speaker, which costs 74. What does this query return?

  5. 5. Complete the code

    Orders of 3 units or more ship in a big box. Fill in the blank to find them.

  6. 6. Complete the code

    A customer asks what you have in Audio. Fill in the blank in the condition.

  7. 7. Find the bug

    This should show the name and the age of whoever lives in Tampa. SQL answers that there's no column Tampa, and nobody asked it for a column by that name. Which line has the error?

  8. 8. Multiple choice

    Someone in a hurry writes WHERE city = 'denver' here, all in lowercase. The table says Denver. What happens?

  9. 9. Complete the code

    Half of the orders are for a single unit. Fill in the blank to see the others, the ones with any quantity that isn't 1.

  10. 10. Predict the output

    There are 8 people in customers. Three live in Denver, and nobody knows Chloë's city. SQL only lets a row through when it can be sure the condition is true. What does this query return?

  11. 11. Put the lines in order

    After the clearance sale on everything, this week there's another one, Audio only. Put in order the query that shows each Audio product with its price cut in half. One line doesn't belong.

  12. 12. Write the code

    Shipping is free for anything that costs 100 or more. Write a query that shows the name and the price of those products, in that order.

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 →