Loading lessons...
AND, OR, NOT
AND, OR, NOT
The WHERE clause can combine several conditions with logical operators.
AND - all conditions must be true
SELECT * FROM Customers
WHERE Country = 'Germany' AND City = 'Berlin';
Only rows that are both in Germany and in Berlin match.
OR - at least one condition must be true
SELECT * FROM Customers
WHERE City = 'Berlin' OR City = 'London';
Rows from Berlin or London are returned.
NOT - the opposite
SELECT * FROM Customers
WHERE NOT Country = 'Germany';
Returns every country except Germany.
Combining them
Parentheses control the order of evaluation, just like math:
SELECT * FROM Customers
WHERE Country = 'Germany' AND (City = 'Berlin' OR City = 'Munich');
This reads: in Germany, and (in Berlin or Munich).
NOT with IN
SELECT * FROM Customers
WHERE Country NOT IN ('Germany', 'France');
Excludes Germany and France.
TL;DR
- AND: all conditions must be true.
- OR: one condition must be true.
- NOT: reverses a condition.
- Use parentheses to group conditions.