Filtering rows is the most common thing you do in SQL, and the WHERE clause is deceptively deep — especially where NULL is concerned, which trips up almost everyone. This chapter covers the full toolkit for selecting exactly the rows you want.
Comparison & logical operators
SELECT * FROM studentsWHERE package > 6AND enrolled >= '2026-01-01'AND (programme = 'Data' OR programme = 'Full Stack');-- Operators: = <> < <= > >= and AND / OR / NOT-- Parenthesise mixed AND/OR — precedence gives AND before OR.
LIKE patterns
LIKE matches text patterns with two wildcards: % (any sequence) and _ (one character):
WHERE name LIKE 'A%' -- starts with AWHERE email LIKE '%@gmail.com' -- ends withWHERE name LIKE '%aan%' -- containsWHERE code LIKE 'D_2%' -- D, any char, 2, then anything-- ILIKE (Postgres) is case-insensitive; elsewhere use LOWER(name) LIKE …
IN & BETWEEN
WHERE programme IN ('Data', 'Full Stack', 'DevOps') -- any of a listWHERE programme NOT IN ('Archived')WHERE package BETWEEN 5 AND 8 -- inclusive range (5 ≤ x ≤ 8)WHERE enrolled BETWEEN '2026-01-01' AND '2026-03-31';
Handling NULL
NULL means "unknown", and it does not behave like a value. Any comparison to NULL (even = NULL) yields "unknown", never true — so you must use IS NULL / IS NOT NULL:
WHERE email IS NULL -- ✓ finds missing emailsWHERE email = NULL -- ✗ NEVER matches anything-- NULL also drops out of other conditions:WHERE package <> 8 -- excludes rows where package IS NULL too!-- To keep them:WHERE package <> 8 OR package IS NULL;
Practice
On a students table, write filters for: names starting with "A", packages between 6 and 9, a programme in a list of three, students with no recorded email, and everyone whose package isn't 8 including those with an unknown package.
Key takeaways
- 01Combine conditions with AND/OR/NOT — parenthesise when mixing (AND binds tighter than OR).
- 02LIKE matches patterns: % (any sequence), _ (one char); use LOWER()/ILIKE for case-insensitivity.
- 03IN tests membership in a list; BETWEEN tests an inclusive range.
- 04NULL means unknown — comparisons to it are never true; use IS NULL / IS NOT NULL.
- 05NULLs silently drop out of <>/>/< filters — handle them explicitly.
