AND, OR and NOT

Updated · 9 min read
easyPostgreSQL 18sqlwhereandornotlogicinterview

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.
  • NOT flips a condition: true becomes false and false becomes true.
  • AND binds tighter than OR. Use parentheses whenever you mix them.
  • With NULL, a condition can be unknown, and NOT unknown is still unknown, so the row is left out.

The Basics

SQL
WHERE condition1 AND condition2
WHERE condition1 OR condition2
WHERE NOT condition
OperatorKeeps the row whenExample
ANDBoth sides are truetechnology = 'Java' AND level = 'Beginner'
ORAt least one side is truestate = 'CA' OR state = 'TX'
NOTThe condition is falseNOT technology = 'Java'

Java courses that are also for beginners:

SQL▶ Try it
SELECT title, technology, level
FROM courses
WHERE technology = 'Java' AND level = 'Beginner'
ORDER BY title;
Output
    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 keeps the overlap; OR keeps either; NOT keeps the outside

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:

SQL▶ Try it
SELECT title, technology, fee_usd
FROM courses
WHERE technology = 'Java' OR technology = 'SQL' AND fee_usd > 450
ORDER BY title;
Output
      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:

SQL▶ Try it
SELECT title, technology, fee_usd
FROM courses
WHERE (technology = 'Java' OR technology = 'SQL') AND fee_usd > 450
ORDER BY title;
Output
      title       | technology | fee_usd
------------------+------------+---------
 Java Collections | Java       |  550.00
 Java OOP         | Java       |  500.00
(2 rows)
Without parentheses, AND groups first and the meaning changes

NOT

NOT turns a condition around. Courses that aren't Java:

SQL▶ Try it
SELECT title, technology
FROM courses
WHERE NOT technology = 'Java'
ORDER BY title;
Output
       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":

SQL▶ Try it
SELECT title, technology, level
FROM courses
WHERE NOT (technology = 'Java' AND level = 'Beginner')
ORDER BY title;
Output
       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)
SQL▶ Try it
SELECT title, technology, level
FROM courses
WHERE technology <> 'Java' OR level <> 'Beginner'
ORDER BY title;
Output
       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.

OriginalTurned 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:

ExpressionResultWhy
unknown AND falsefalseOne false side is enough to make AND false
unknown AND trueunknownIt depends on the unknown side
unknown OR truetrueOne true side is enough to make OR true
unknown OR falseunknownIt depends on the unknown side
NOT unknownunknownFlipping an unknown is still unknown

You can check these in PostgreSQL, which shows unknown as NULL:

SQL▶ Try it
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;
Output
 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:

SQL▶ Try it
SELECT payment_id, method
FROM payments
WHERE NOT method = 'card'
ORDER BY payment_id;
Output
 payment_id | method
------------+--------
          3 | check
          7 | check
(2 rows)
true, false and unknown, combined; WHERE keeps only true

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
SQL▶ Try it
SELECT first_name, state, joined_on
FROM students
WHERE (state = 'CA' OR state = 'TX') AND joined_on < '2025-06-01'
ORDER BY joined_on;
Output
 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.

Quick check: AND, OR and NOT

3 questions
1What does WHERE technology = 'Java' AND level = 'Beginner' return?
2How does the database read a OR b AND c?
3Payment 5's method is NULL. Is it returned by WHERE NOT method = 'card'?

Key Takeaways

You want toWrite
Both conditionsWHERE a AND b
Either conditionWHERE a OR b
Mix them safelyWHERE (a OR b) AND c
The oppositeWHERE NOT (a AND b)
Include missing values... OR column IS NULL

Was this tutorial helpful?

Lesson 16 of 115