AND, OR and NOT
Most real questions have more than one condition. "Java courses for beginners" needs two conditions that must both hold; "Java or SQL courses" needs either one. AND, OR and NOT combine and flip conditions inside a WHERE. They're easy to read and easy to get wrong, because AND is applied before OR unless you add parentheses. This lesson shows how each one works, the parentheses rule, how to turn a condition around, and what a missing value does to each.
In One Minute
AND: the row stays only if both conditions are true.OR: the row stays if at least one condition is true.NOTflips a condition: true becomes false and false becomes true.ANDbinds tighter thanOR. Use parentheses whenever you mix them.- With
NULL, a condition can be unknown, andNOT unknownis still unknown, so the row is left out.
The Basics
WHERE condition1 AND condition2
WHERE condition1 OR condition2
WHERE NOT condition| Operator | Keeps the row when | Example |
|---|---|---|
AND | Both sides are true | technology = 'Java' AND level = 'Beginner' |
OR | At least one side is true | state = 'CA' OR state = 'TX' |
NOT | The condition is false | NOT technology = 'Java' |
Java courses that are also for beginners:
SELECT title, technology, level
FROM courses
WHERE technology = 'Java' AND level = 'Beginner'
ORDER BY title; title | technology | level
-------------+------------+----------
Java Basics | Java | Beginner
(1 row)Only Java Basics passes both tests. Java OOP is Java but not a beginner course, and SQL Basics is a beginner course but not Java.
AND Before OR: Use Parentheses
When a condition mixes AND and OR, SQL applies AND first, just as arithmetic does multiplication before addition. Say you want Java or SQL courses that cost more than $450. This looks right, but isn't:
SELECT title, technology, fee_usd
FROM courses
WHERE technology = 'Java' OR technology = 'SQL' AND fee_usd > 450
ORDER BY title; title | technology | fee_usd
------------------+------------+---------
Java Basics | Java | 450.00
Java Collections | Java | 550.00
Java OOP | Java | 500.00
(3 rows)Java Basics costs exactly $450.00, yet it's in the result. Because AND binds tighter, the database read the condition as technology = 'Java' OR (technology = 'SQL' AND fee_usd > 450): every Java course, plus SQL courses over $450. Parentheses say what you mean:
SELECT title, technology, fee_usd
FROM courses
WHERE (technology = 'Java' OR technology = 'SQL') AND fee_usd > 450
ORDER BY title; title | technology | fee_usd
------------------+------------+---------
Java Collections | Java | 550.00
Java OOP | Java | 500.00
(2 rows)NOT
NOT turns a condition around. Courses that aren't Java:
SELECT title, technology
FROM courses
WHERE NOT technology = 'Java'
ORDER BY title; title | technology
-------------------+------------
Git Basics | Git
JavaScript Basics | JavaScript
SQL Basics | SQL
(3 rows)NOT technology = 'Java' means the same as technology <> 'Java'. NOT is most useful in front of a longer condition in parentheses, or with operators you'll meet next, such as NOT IN, NOT BETWEEN, NOT LIKE and IS NOT NULL.
Turning a Condition Around
To flip a combined condition, flip each part and swap AND with OR. This rule is known as De Morgan's laws. "Not (Java and Beginner)" means "not Java, or not Beginner":
SELECT title, technology, level
FROM courses
WHERE NOT (technology = 'Java' AND level = 'Beginner')
ORDER BY title; title | technology | level
-------------------+------------+--------------
Git Basics | Git | Beginner
Java Collections | Java | Intermediate
Java OOP | Java | Intermediate
JavaScript Basics | JavaScript | Beginner
SQL Basics | SQL | Beginner
(5 rows)SELECT title, technology, level
FROM courses
WHERE technology <> 'Java' OR level <> 'Beginner'
ORDER BY title; title | technology | level
-------------------+------------+--------------
Git Basics | Git | Beginner
Java Collections | Java | Intermediate
Java OOP | Java | Intermediate
JavaScript Basics | JavaScript | Beginner
SQL Basics | SQL | Beginner
(5 rows)Both return the five courses that aren't Java Basics.
| Original | Turned around |
|---|---|
NOT (a AND b) | NOT a OR NOT b |
NOT (a OR b) | NOT a AND NOT b |
What NULL Does to AND, OR and NOT
With NULL, a condition can be unknown instead of true or false, and AND, OR and NOT follow fixed rules for unknown:
| Expression | Result | Why |
|---|---|---|
unknown AND false | false | One false side is enough to make AND false |
unknown AND true | unknown | It depends on the unknown side |
unknown OR true | true | One true side is enough to make OR true |
unknown OR false | unknown | It depends on the unknown side |
NOT unknown | unknown | Flipping an unknown is still unknown |
You can check these in PostgreSQL, which shows unknown as NULL:
SELECT NULL AND false AS a, NULL AND true AS b, NULL OR true AS c, NULL OR false AS d, NOT NULL AS e; a | b | c | d | e
---+------+---+------+------
f | NULL | t | NULL | NULL
(1 row)The last rule has a practical effect. Payment 5 has no method, so NOT method = 'card' is NOT unknown, which is still unknown, and payment 5 is left out:
SELECT payment_id, method
FROM payments
WHERE NOT method = 'card'
ORDER BY payment_id; payment_id | method
------------+--------
3 | check
7 | check
(2 rows)NOT doesn't rescue missing values. To include them, say so: WHERE NOT method = 'card' OR method IS NULL.
Common Mistakes
Mixing AND and OR without parentheses. a OR b AND c means a OR (b AND c). Add parentheses to say what you mean.
Writing state = 'CA' OR 'TX'. Each side of OR must be a complete condition: state = 'CA' OR state = 'TX', or state IN ('CA', 'TX'), which you'll meet in IN and BETWEEN.
Using AND where you mean OR. state = 'CA' AND state = 'TX' can never be true; a student lives in one state.
Expecting NOT to include missing values. NOT method = 'card' leaves out NULL methods, just like method <> 'card'.
Interview Corner
Which binds tighter in SQL, AND or OR?
AND. So a OR b AND c means a OR (b AND c). Use parentheses whenever you mix them.
What is NULL AND FALSE? NULL OR TRUE?
NULL AND FALSE is false, because one false side makes AND false whatever the other side is. NULL OR TRUE is true for the same reason. NULL AND TRUE and NULL OR FALSE stay unknown.
How do you write NOT (a OR b) without the NOT outside?
NOT a AND NOT b, by De Morgan's laws: flip each part and swap OR with AND.
Practice
Challenge: List the students who live in California or Texas and joined before 2025-06-01. Show first_name, state and joined_on, sorted by joined_on. Use parentheses.
Show solution
SELECT first_name, state, joined_on
FROM students
WHERE (state = 'CA' OR state = 'TX') AND joined_on < '2025-06-01'
ORDER BY joined_on; first_name | state | joined_on
------------+-------+------------
Srihitha | CA | 2024-08-19
Advitha | CA | 2025-01-13
(2 rows)Srihitha and Advitha joined before June 1, 2025; Navika joined in September. Without the parentheses, the date test would apply only to Texas.
Key Takeaways
| You want to | Write |
|---|---|
| Both conditions | WHERE a AND b |
| Either condition | WHERE a OR b |
| Mix them safely | WHERE (a OR b) AND c |
| The opposite | WHERE NOT (a AND b) |
| Include missing values | ... OR column IS NULL |
Was this tutorial helpful?