Filtering with WHERE
WHERE keeps only the rows that match a condition. It's how you find one user out of millions.
Text values go in single quotes. Numbers don't need quotes.
Comparison operators
| Operator | Meaning |
|---|---|
= |
equal |
<> or != |
not equal |
> < >= <= |
greater / less than |
Combining conditions: AND, OR, NOT
Precedence trap:
ANDbinds tighter thanOR. When you mix them, always use brackets:WHERE category = 'Kitchen' AND (price < 10 OR stock > 150)
IN, BETWEEN and LIKE
| Pattern | Matches |
|---|---|
'A%' |
starts with A |
'%son' |
ends with "son" |
'%lap%' |
contains "lap" |
'_i%' |
second letter is i |
NULL: the missing value
NULL means "unknown or missing". It is not equal to anything, not even another NULL, so = NULL never works. Use IS NULL / IS NOT NULL.
The statements run in order. The first SELECT matches no rows, so only one result table appears: the one from IS NULL.
Practice
Find all products that are in stock, cost between 5 and 50, and are not in the Kitchen category.
✎ Check your understanding
1.Which query finds customers with no email?
2.Which names match LIKE 'M%'?