SQL AND, OR and NOT
AND, OR and NOT combine conditions in WHERE. AND needs both sides to be true, OR needs either, and NOT flips a condition.
Updated December 1, 2026
Syntax
WHERE condition1 AND condition2
WHERE condition1 OR condition2
WHERE NOT condition
AND: every condition must match
Coffee that costs less than $4.50.
SELECT name, price
FROM drinks
WHERE category = 'coffee' AND price < 4.5;
| name | price |
|---|---|
| Espresso | 3 |
| Cappuccino | 4.25 |
Use parentheses with OR
AND binds tighter than OR, like multiplication before addition. Parentheses make sure the OR is checked first: here, caffeine-free drinks that are tea or “other”.
SELECT name, category
FROM drinks
WHERE (category = 'tea' OR category = 'other')
AND caffeinated = 0;
| name | category |
|---|---|
| Peppermint Tea | tea |
| Hot Chocolate | other |
| Lemonade | other |
NOT: exclude matches
NOT reverses a condition. It reads well with IN, LIKE and BETWEEN.
SELECT name, category
FROM drinks
WHERE NOT category = 'coffee';
| name | category |
|---|---|
| Matcha Latte | tea |
| Chai Latte | tea |
| Peppermint Tea | tea |
| Hot Chocolate | other |
| Lemonade | other |
Common mistakes
- Leaving out parentheses:
WHERE category = 'tea' OR category = 'other' AND caffeinated = 0means tea, or caffeine-free “other”. - Writing
WHERE category = 'tea' OR 'other'. Repeat the column, or useIN ('tea', 'other'). NOTdoesn’t match rows where the value is NULL, because NULL is neither equal nor unequal.
Practice AND, OR and NOT interactively
Write real queries in your browser and get instant feedback in the lesson “AND, OR, NOT” (Unit 2). New learners can start free from the first lesson.
Frequently asked questions
Which runs first, AND or OR?
NOT first, then AND, then OR. Add parentheses whenever you mix AND and OR, even if you think the default is right.
Is there a shorter way to write many ORs?
Yes: category IN ('tea', 'other') means the same as two OR conditions. See IN.