SQL
5 min read
BEGINNER
AND OR NOT
Quick AnswerAND requires both conditions, OR requires at least one, and NOT negates a condition. These descriptions assume true or false inputs; SQL also has UNKNOWN.
Core idea
AND requires both conditions, OR requires at least one, and NOT negates a condition. These descriptions assume true or false inputs; SQL also has UNKNOWN.
Minimal example
sql
SELECT id FROM products WHERE price_cents < 350 AND NOT (name = 'Pen') ORDER BY id;Expected SQLite rows:
text
4How it works
NOT binds before AND, and AND before OR. Parentheses make grouping explicit. Compare the second example with the same query without parentheses: without them the result includes ID 1 as well.
Practical example
sql
SELECT id FROM products WHERE (id = 1 OR id = 2) AND price_cents < 350 ORDER BY id;Expected SQLite rows:
text
2Common mistake and practice
Do not assume AND and OR evaluate from left to right. Find products that cost less than 350 cents and are not Pen; expect ID 4.