Query data
Filter rows with WHERE
Write precise conditions with comparison, logical, range, set, pattern, and NULL operators.
8 minute lesson
A WHERE clause keeps rows where its condition is true. False and unknown conditions are both excluded.
Suppose people contains:
| name | active | age | |
|---|---|---|---|
| Ada | true | 36 | ada@example.com |
| Lin | true | 15 | NULL |
| Sam | false | 42 | sam@example.com |
This query returns only Ada:
SELECT name, email
FROM people
WHERE active = TRUE AND age >= 18;
SQL has three-valued logic: true, false, and unknown. A comparison with NULL normally produces unknown:
WHERE email = NULL
That condition does not find missing email addresses. Use:
WHERE email IS NULL
The same issue appears with inequality. email <> 'ada@example.com' returns Sam, but not Lin, because Lin’s comparison is unknown. Use email <> 'ada@example.com' OR email IS NULL when missing values belong in the result.
Parentheses make mixed conditions explicit:
WHERE active = TRUE
AND (country = 'Italy' OR country = 'Denmark')
Without the parentheses, operator precedence can include inactive rows from one country.
Use IN for a set of values, BETWEEN for an inclusive range, and LIKE for a pattern when those forms express the requirement clearly. They do not remove the need to reason about NULL.
Your action is to predict which three sample rows match four conditions: age >= 18, email IS NULL, email <> 'ada@example.com', and active = TRUE OR age >= 18. Then run the queries.
Lesson completed