SQL WHERE
WHERE filters rows. Only rows where the condition evaluates to TRUE appear in the result.
Comparison operators
| Operator | Means |
|---|---|
= | Equal |
<> or != | Not equal |
<, >, <=, >= | Magnitude |
BETWEEN a AND b | In a range, inclusive |
IN (x, y, z) | In a list |
LIKE 'A%' | Pattern match |
IS NULL / IS NOT NULL | Null check |
Combining conditions
SQL
SELECT * FROM products WHERE price > 50 AND stock > 0 AND (category = 'books' OR category = 'music');
Quotes matter
- String values are wrapped in single quotes:
'AU'. - Numbers and booleans are unquoted:
42,true. - Identifiers (table/column names) use backticks in MySQL or double quotes in PostgreSQL/standard SQL.
Tip: Never compare to
NULL with = — it never returns true. Always use IS NULL / IS NOT NULL.Example
Exercise
Filter for products that cost more than 50.
SELECT * FROM products
price > 50;
Five letters; introduces the filter clause.
Discussion
Loading…