AND, OR and NOT in SQL
AND keeps rows where all conditions are true, OR keeps rows where at least one is true, and NOT reverses a condition. When you mix AND with OR, use brackets, because AND is evaluated first.
Why do brackets matter?
You want paid orders from Austria or Germany:
WHERE country = 'AT' OR country = 'DE' AND status = 'paid'
This returns all Austrian orders, paid or not, because AND binds first. Correct:
WHERE (country = 'AT' OR country = 'DE') AND status = 'paid'
Is there a shorter way to write OR?
Yes, IN:
WHERE country IN ('AT', 'DE') AND status = 'paid'
How do you exclude something?
WHERE status <> 'cancelled'
<> and != both mean "not equal" in most databases. <> is the SQL standard.
The NULL trap
WHERE status <> 'cancelled' also drops rows where status is NULL, because a comparison with NULL is never true. If those rows should stay:
WHERE status <> 'cancelled' OR status IS NULL
Practise AND, OR, NOT Practise not equal
Updated November 2026.