Filtering and Sorting · Lesson 10 of 64

SQL AND, OR and NOT

What you will learn

  • How AND needs both conditions and OR needs one
  • How NOT reverses a condition
  • Why AND is evaluated before OR
  • How to use brackets to make the meaning clear

This lesson runs on the sample database you chose in lesson 1.1 (use the Example data picker at the top). Edit any query and press Run. Reset data puts the original rows back.

1. AND: both must be true

AND keeps a row only when both conditions are true:

SELECT customer_id, first_name, country, is_active FROM customers
WHERE country = 'Kenya' AND is_active = 1 ORDER BY customer_id LIMIT 5;
customer_id  first_name  country  is_active
-----------  ----------  -------  ---------
         17  Bela        Kenya            1
         37  Priya       Kenya            1
         64  Wei         Kenya            1
         98  Dev         Kenya            1
        154  Vik         Kenya            1
(5 rows)
SELECT account_id, account_type, status, balance_cents FROM accounts
WHERE account_type = 'savings' AND status = 'open' ORDER BY account_id LIMIT 5;
account_id  account_type  status  balance_cents
----------  ------------  ------  -------------
         2  savings       open            52078
         3  savings       open            62780
         8  savings       open           122464
         9  savings       open           110160
        10  savings       open            79850
(5 rows)
SELECT course_id, code, credits, department_id FROM courses
WHERE credits >= 4 AND department_id = 1 ORDER BY course_id;
course_id  code     credits  department_id
---------  -------  -------  -------------
        1  MATH101        4              1
        3  MATH301        4              1
(2 rows)

Adding more conditions with AND makes the result smaller: each one removes more rows.

2. OR: one is enough

OR keeps a row when at least one condition is true:

SELECT customer_id, first_name, city FROM customers
WHERE city = 'Leeds' OR city = 'Cairo' ORDER BY customer_id LIMIT 6;
customer_id  first_name  city
-----------  ----------  -----
          1  Vik         Leeds
          2  Wanda       Leeds
          3  Dana        Cairo
          6  Uma         Leeds
          7  Ugo         Leeds
         23  Bruno       Cairo
(6 rows)
SELECT account_id, status FROM accounts
WHERE status = 'frozen' OR status = 'closed' ORDER BY account_id LIMIT 6;
account_id  status
----------  ------
         4  closed
         7  closed
        17  frozen
        18  frozen
        51  closed
        88  frozen
(6 rows)
SELECT student_id, first_name, enrolled_year FROM students
WHERE enrolled_year = 2023 OR enrolled_year = 2026 ORDER BY student_id LIMIT 6;
student_id  first_name  enrolled_year
----------  ----------  -------------
         1  Vera                 2026
         4  Quinn                2023
         6  Nadia                2026
         7  Jin                  2023
         9  Sven                 2026
        11  Olga                 2023
(6 rows)

Adding conditions with OR makes the result bigger: each one lets more rows in.

3. NOT: the opposite

NOT reverses a condition. NOT (a = b) means the same as a <> b; it is most useful in front of a bracketed group:

SELECT status, COUNT(*) AS orders FROM orders
WHERE NOT (status = 'delivered' OR status = 'cancelled') GROUP BY status ORDER BY status;
status   orders
-------  ------
paid        186
pending     135
shipped     231
(3 rows)
SELECT status, COUNT(*) AS accounts FROM accounts
WHERE NOT (status = 'open') GROUP BY status ORDER BY status;
status  accounts
------  --------
closed        30
frozen        16
(2 rows)
SELECT enrolled_year, COUNT(*) AS students FROM students
WHERE NOT (enrolled_year = 2023 OR enrolled_year = 2024) GROUP BY enrolled_year ORDER BY enrolled_year;
enrolled_year  students
-------------  --------
         2025        86
         2026        89
(2 rows)

4. AND before OR: the trap

When AND and OR appear together without brackets, SQL evaluates AND first, as multiplication comes before addition. This often surprises beginners. Compare the two queries; they differ only in the brackets:

Customers from Leeds or Cairo who are active:

SELECT COUNT(*) AS without_brackets FROM customers
WHERE city = 'Leeds' OR city = 'Cairo' AND is_active = 1;
without_brackets
----------------
              59
(1 row)
SELECT COUNT(*) AS with_brackets FROM customers
WHERE (city = 'Leeds' OR city = 'Cairo') AND is_active = 1;
with_brackets
-------------
           56
(1 row)

Savings or checking accounts that are open:

SELECT COUNT(*) AS without_brackets FROM accounts
WHERE account_type = 'savings' OR account_type = 'checking' AND status = 'open';
without_brackets
----------------
             418
(1 row)
SELECT COUNT(*) AS with_brackets FROM accounts
WHERE (account_type = 'savings' OR account_type = 'checking') AND status = 'open';
with_brackets
-------------
          404
(1 row)

Students enrolled in 2025 or 2026 whose guardian is Amir Khan:

SELECT COUNT(*) AS without_brackets FROM students
WHERE enrolled_year = 2025 OR enrolled_year = 2026 AND guardian_name = 'Amir Khan';
without_brackets
----------------
              87
(1 row)
SELECT COUNT(*) AS with_brackets FROM students
WHERE (enrolled_year = 2025 OR enrolled_year = 2026) AND guardian_name = 'Amir Khan';
with_brackets
-------------
            2
(1 row)

The first query was read as "this OR (that AND the other)", so it let in all the rows of the first condition. The second says what was meant. Always use brackets when you mix AND and OR, even when you are sure of the order: they make the query right and readable.

Worked example: say it in words first

"Show me products that are cheap or on sale, but only the ones in stock."

Wrong reading Right reading
cheap OR on_sale AND in_stock means cheap (any stock) or (on sale and in stock) (cheap OR on_sale) AND in_stock means in stock, and either cheap or on sale

When a condition has an "or" inside an "and", write the "or" part in brackets. The columns in the illustration are made up.

Common mistakes

  • Mixing AND and OR without brackets.
  • Using OR to build a long list of values. Lesson 2.3 shows IN.
  • Thinking more ANDs give more rows. They give fewer.
  • Writing WHERE a = 1 OR 2. Each side of OR must be a full condition: a = 1 OR a = 2.

Key takeaways

  • AND needs both conditions; OR needs at least one; NOT reverses.
  • AND binds tighter than OR; use brackets whenever you mix them.
  • More ANDs shrink the result; more ORs grow it.

Practice

1. Write a condition for "savings accounts that are open".

Show answer

WHERE account_type = 'savings' AND status = 'open'

2. What is wrong with WHERE city = 'Leeds' OR 'Cairo'?

Show answer

Each side of OR must be a full condition: city = 'Leeds' OR city = 'Cairo'. (Lesson 2.3 shows city IN ('Leeds', 'Cairo').)

3. Which is evaluated first in a OR b AND c?

Show answer

b AND c. AND binds tighter than OR.

4. Rewrite WHERE NOT (status = 'open') with <>.

Show answer

WHERE status <> 'open'

5. Does adding another AND condition make the result larger or smaller?

Show answer

Smaller or the same: every row must meet one more condition.

6. Why use brackets even when you know the order?

Show answer

They make the meaning clear to the next reader and protect against a wrong guess about the order.