WHERE filters rows. Only rows for which the condition is true are returned:
SELECT name, price
FROM products
WHERE price > 20;
Comparisons
| Operator | Meaning |
|---|---|
= | equal to |
<> or != | not equal to |
> and < | greater than, less than |
>= and <= | greater or equal, less or equal |
Text needs quotes
Numbers are written as they are. Text values go in single quotes:
SELECT name FROM products WHERE category = 'Home';
The text inside the quotes must match exactly, including capital letters: 'home' would find nothing.
Several conditions
Combine conditions with AND (both must be true) and OR (at least one must be true):
SELECT name, price, stock
FROM products
WHERE category = 'Home' AND price < 20;
When you mix AND and OR, use brackets to make clear what belongs together:
WHERE (category = 'Home' OR category = 'Travel') AND stock > 0
Without the brackets, AND is applied first, which is rarely what you meant.
Tables in the practice database
| Table | Columns |
|---|---|
customers | id, name, city, joined |
products | id, name, category, price, stock |
orders | id, customer_id, product_id, quantity, ordered |
Every run starts from a fresh copy of the database, so nothing you do can break it.
Your Task
- Write a query that returns the
name,priceandstockof all products in theStationerycategory that are in stock (astockgreater than 0).
- The query uses
WHERE - The result has the columns
name,priceandstock - Only Stationery products that are in stock are returned
Lesson complete!
Next Lesson: Sorting and Limiting