AND, OR, and NOT
SQL · Unit 2: Filtering
- Full plan
- SQL
- 11 exercises
A customer writes in: I want something in Audio, but something you have today. That's two conditions, and AND joins them inside the same WHERE.
A row gets through only if both are true. The Speaker is in Audio, but there are none left, so it doesn't show up.
SELECT name, price
FROM products
WHERE category = 'Audio'
AND stock > 0;Returns
| name | price |
|---|---|
| Headphones | 59.9 |
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. Predict the output
The store is planning a campaign for its Denver customers who are under 40. What does this query return?
2. Complete the code
Someone is looking for a device for under 500. Fill in the blank.
3. Put the lines in order
Luke has id 2 in
customers. Which of his orders are for more than one unit? Put the query in order. One line doesn't belong.4. Complete the code
A customer can't decide between something in Audio and something in Devices, and wants to see both categories. Fill in the blank.
5. Predict the output
The Speaker is in Audio and it's also out of stock. What does this query return?
6. Find the bug
There's a promotion for people who live in Denver and for people who live in Austin. The query runs without an error and returns nobody. Which line has the problem?
7. Predict the output
Someone wants a device or a display that costs under 200. Whoever wrote the query forgot the parentheses. What does it return?
8. Find the bug
The stock clerk is checking the accessories that are out of stock or have more than 20 in stock. An Audio product slipped into the result. Which line needs fixing?
9. Predict the output
The stock clerk wants to see everything with no more than 5 units, and wrote it with a
NOT. What does this query return?10. Put the lines in order
The end-of-month catalog leaves out what's out of stock, and the accessories too. Put in order the query that shows what does make it in. One line doesn't belong.
11. Write the code
The customer who couldn't decide between Audio and Devices now asks to see only what's in stock. The query already returns both categories. Add that condition so that it applies to both, and finish the query. It should show the
nameand thestock, in that order.
It opens in your browser. This lesson is part of the full plan; the first unit of each course is free.