The WHERE clause keeps only the rows that satisfy a condition. Mastering its operators — comparisons, logical combinations, ranges, lists, patterns and the special handling of NULL — covers the majority of everyday SQL.
Comparison & logical operators
Compare with = <> < > <= >= and combine conditions with AND, OR and NOT. Use parentheses to make precedence explicit.
Filtering with conditions
SELECT name, salary FROM employees WHERE dept_id = 10 AND salary >= 60000; SELECT name FROM employees WHERE dept_id = 10 OR dept_id = 20;
Ranges, lists and patterns
BETWEEN, IN and LIKE
WHERE salary BETWEEN 50000 AND 70000 -- inclusive range WHERE dept_id IN (10, 30) -- membership list WHERE name LIKE 'A%' -- starts with A WHERE name LIKE '_a%' -- 2nd letter is a
Handling NULL
WHERE manager_id IS NULL -- correct WHERE manager_id = NULL -- WRONG: never true
⚡ The edge
- NULL means 'unknown', so it never equals anything — use IS NULL / IS NOT NULL, never = NULL.
- LIKE patterns use % (any number of characters) and _ (exactly one). IN (...) is a clean shorthand for many OR conditions.
Worked example
Find employees in department 10 or 20 who earn more than 55000.
- Two department options: dept_id IN (10, 20).
- Plus a salary condition: salary > 55000.
- Combine with AND, parenthesising the IN for clarity.
Answer: SELECT name, salary FROM employees WHERE dept_id IN (10,20) AND salary > 55000;
Worked example
List employees whose name starts with the letter A.
- A 'starts with' pattern uses LIKE with a trailing %.
- The pattern 'A%' matches any name beginning with A.
- Note LIKE may be case-sensitive depending on the database/collation.
Answer: SELECT name FROM employees WHERE name LIKE 'A%';
⚠ Watch out
- = NULL is always unknown (never true) — use IS NULL.
- Mixing AND/OR without parentheses can change the meaning — AND binds tighter than OR.
- BETWEEN is inclusive of both endpoints.