Comparison operators
Inside WHERE you compare values with these operators:
| Operator | Meaning |
|---|---|
= |
equal |
<> |
not equal |
< |
less than |
<= |
less than or equal |
> |
greater than |
>= |
greater than or equal |
SELECT name, price FROM products WHERE price >= 100;
SELECT name FROM products WHERE category <> 'audio';
Logical operators: AND, OR, NOT
They combine several conditions in the same WHERE:
AND: both conditions are met.OR: at least one is met.NOT: negates a condition (inverts it).
-- Peripherals that also cost less than 30
SELECT * FROM products
WHERE category = 'peripherals' AND price < 30;
-- Audio or display products
SELECT * FROM products
WHERE category = 'audio' OR category = 'displays';
-- Everything that is NOT audio
SELECT * FROM products
WHERE NOT category = 'audio';
Precedence and parentheses
AND has higher priority than OR (it is evaluated first). When you mix
the two, use parentheses to make the grouping clear and avoid surprises:
-- Without parentheses, AND "binds" first:
-- category='audio' OR (category='displays' AND price < 200)
SELECT * FROM products
WHERE category = 'audio' OR category = 'displays' AND price < 200;
-- With parentheses you control the order:
SELECT * FROM products
WHERE (category = 'audio' OR category = 'displays') AND price < 200;
Practical rule: as soon as you mix
ANDandOR, add parentheses. The query becomes more readable and you make your intention explicit.