AND, OR, NOT
AND keeps a row only when both conditions are true, OR when at least one is true, NOT reverses a condition. AND binds tighter than OR, so a OR b AND c means a OR (b AND c); use parentheses when mixing them: WHERE (country = 'Aruba' OR country = 'Curacao') AND status = 'paid'.
Syntax
WHERE condition1 AND condition2 | WHERE condition1 OR condition2 | WHERE NOT conditionExamples
SELECT * FROM orders WHERE status = 'paid' AND amount > 100;Result: Paid orders above 100
SELECT * FROM orders WHERE (status = 'paid' OR status = 'shipped') AND amount > 100;Result: Paid or shipped orders above 100
Without the parentheses every shipped order would be included, regardless of amount.
SELECT name FROM customers WHERE NOT country = 'Aruba';Result: Customers outside Aruba (rows with NULL country are excluded)
Same as country <> 'Aruba'.
Common mistakes
- WHERE country = 'Aruba' OR 'Curacao' is wrong: the second part is just a string; each side of OR needs a full comparison (or use IN).
- Mixing AND and OR without parentheses silently applies AND first; always group OR conditions in parentheses.
- NOT IN and NOT LIKE on NULL columns drop the NULL rows because NOT NULL is still NULL; add IS NULL checks if needed.
Practice AND, OR, NOT
These drills check your answer on live data, free, no account needed.
- Filter rowsReturn the name and salary of employees who earn more than 90,000.
- Two countriesReturn the name and country of customers based in the US or the Netherlands (NL).
Questions
Which comes first, AND or OR?
AND is evaluated before OR, like multiplication before addition. NOT is evaluated before both. Parentheses override the order.
Is there a limit on how many conditions I can combine?
No practical limit. For long OR lists on the same column use IN, which is shorter and easier to read.
Related
- WHEREUse WHERE to keep only the rows that match a condition: equals, greater than, text matches, NULL checks, and combinations with AND, OR and NOT.
- INWHERE column IN (...) keeps rows whose value appears in a list or in a subquery result. Examples, NOT IN, the NULL trap, and when to use EXISTS instead.
- IS NULLNULL means unknown, so = NULL never matches. Use IS NULL and IS NOT NULL to find or exclude missing values, and see how NULL behaves in counts and sorting.
- BETWEENBETWEEN low AND high keeps values from low to high, both ends included. Examples with numbers and dates, the end-of-day problem with timestamps, NOT BETWEEN.