AND, OR & NOT

Real questions usually involve more than one condition: products in a category and under a price, orders that are pending or shipped. The logical operators AND, OR and NOT combine conditions — and parentheses make sure they combine the way you mean.

AND: all conditions must be true

SQL
SELECT name, category, price
FROM products
WHERE category = 'Electronics' AND price < 50;
Result 2 rows
namecategoryprice
USB-C ChargerElectronics19.99
Smart PlugElectronics14.99

OR: at least one must be true

SQL
SELECT id, customer_id, status
FROM orders
WHERE status = 'pending' OR status = 'shipped';
Result 5 rows
idcustomer_idstatus
96shipped
111shipped
129pending
133pending
157pending

NOT: reverses a condition

SQL
SELECT name, category
FROM products
WHERE NOT category = 'Electronics';
Result 8 rows
namecategory
The Patient GardenerBooks
Learning SQL Step by StepBooks
Night Sky AtlasBooks
Ceramic MugHome
Desk LampHome
Linen ThrowHome
Jigsaw Puzzle 1000Toys
Wooden Train SetToys

NOT is most useful with other operators: NOT IN, NOT LIKE, NOT BETWEEN, NOT EXISTS and IS NOT NULL, all covered in later lessons.

Precedence: AND before OR

Just as multiplication happens before addition, AND is evaluated before OR. This query looks like "cheap books or toys" but isn't:

SQL
SELECT name, category, price
FROM products
WHERE category = 'Books' OR category = 'Toys' AND price < 20;
Result 4 rows
namecategoryprice
The Patient GardenerBooks24.9
Learning SQL Step by StepBooks32.5
Night Sky AtlasBooks45
Jigsaw Puzzle 1000Toys18

It means category = 'Books' OR (category = 'Toys' AND price < 20), so every book appears regardless of price. Parentheses fix it:

SQL
SELECT name, category, price
FROM products
WHERE (category = 'Books' OR category = 'Toys') AND price < 30;
Result 2 rows
namecategoryprice
The Patient GardenerBooks24.9
Jigsaw Puzzle 1000Toys18
Rule of thumb: whenever a WHERE clause mixes AND and OR, add parentheses — even when they aren't strictly needed, they show your intent.

Truth tables

ABA AND BA OR B
truetruetruetrue
truefalsefalsetrue
falsefalsefalsefalse
trueunknownunknowntrue
falseunknownfalseunknown

SQL has a third truth value, unknown, which appears whenever NULL is compared. WHERE keeps only rows where the condition is true — unknown rows are dropped. Lesson 11 explores what this means in practice.

Several conditions

SQL
SELECT first_name, job_title, salary, hire_date
FROM employees
WHERE department_id = 2
  AND salary >= 68000
  AND hire_date < '2023-01-01';
Result 3 rows
first_namejob_titlesalaryhire_date
MeiEngineering Manager980002016-09-01
JonasSenior Developer850002019-02-18
AmaraDeveloper680002022-05-09

Putting each condition on its own line keeps long filters readable.

Try it yourself

Find customers who are in the UK or France and signed up in 2025.

Try it yourself
Result