TExcelTAD
Home › MySQL › MySQL AND, OR, NOT

MySQL AND, OR, NOT

One condition is often not enough. AND, OR and NOT combine conditions into bigger ones.

Operator The row passes when …
a AND b both a and b are true
a OR b at least one of them is true
NOT a a is false

AND

mysql · try it yourself
SELECT name, unit_price, units_in_stock
FROM products
WHERE unit_price < 3 AND units_in_stock > 80;
+------------------+------------+----------------+
| name             | unit_price | units_in_stock |
+------------------+------------+----------------+
| Butter Croissant |       1.60 |            120 |
| Whole Milk 1L    |       1.45 |            200 |
| Gala Apples 1kg  |       2.99 |             90 |
| Sea Salt Crisps  |       1.99 |            150 |
| Dish Soap 500ml  |       2.30 |            100 |
+------------------+------------+----------------+

Every extra AND narrows the result further.

OR

mysql · try it yourself
SELECT first_name, last_name, country
FROM customers
WHERE country = 'France' OR country = 'Spain';
+------------+-----------+---------+
| first_name | last_name | country |
+------------+-----------+---------+
| Chloe      | Martin    | France  |
| Lucia      | Garcia    | Spain   |
| Olivia     | Moreau    | France  |
+------------+-----------+---------+

Every extra OR widens the result. (For a list of alternatives on one column, country IN ('France', 'Spain') is shorter; see the IN lesson.)

NOT

NOT reverses a condition. It is most often written as part of another operator: NOT IN, NOT LIKE, NOT BETWEEN, IS NOT NULL.

mysql · try it yourself
SELECT order_id, status
FROM orders
WHERE NOT status = 'delivered';
+----------+------------+
| order_id | status     |
+----------+------------+
|        8 | cancelled  |
|       16 | cancelled  |
|       18 | shipped    |
|       19 | shipped    |
|       20 | processing |
|       21 | processing |
|       22 | processing |
+----------+------------+

Precedence: use parentheses

AND binds more tightly than OR, just as * binds more tightly than +. This catches many beginners. Suppose we want UK or German customers who signed up in May 2024 or later:

mysql · try it yourself
-- Wrong: read as  UK  OR  (Germany AND 2024)
SELECT first_name, country, signup_date
FROM customers
WHERE country = 'UK' OR country = 'Germany' AND signup_date >= '2024-05-01';
+------------+---------+-------------+
| first_name | country | signup_date |
+------------+---------+-------------+
| Ben        | UK      | 2024-02-03  |
| Farah      | UK      | 2024-04-28  |
| George     | UK      | 2024-05-19  |
| Noah       | Germany | 2025-01-12  |
+------------+---------+-------------+

That query also returned Ben Carter and Farah Khan, who signed up earlier in 2024, because the date test only applied to Germany. Parentheses fix it:

mysql · try it yourself
SELECT first_name, country, signup_date
FROM customers
WHERE (country = 'UK' OR country = 'Germany') AND signup_date >= '2024-05-01';
+------------+---------+-------------+
| first_name | country | signup_date |
+------------+---------+-------------+
| George     | UK      | 2024-05-19  |
| Noah       | Germany | 2025-01-12  |
+------------+---------+-------------+

Tip: Whenever a WHERE clause mixes AND and OR, add parentheses, even if precedence already happens to be right. The next reader will thank you.

NULL and logic

SQL uses three-valued logic: besides true (1) and false (0) there is unknown (NULL). NULL AND 0 is 0, NULL OR 1 is 1, but NULL AND 1, NULL OR 0 and NOT NULL are all NULL, and a NULL condition does not pass a WHERE filter.

mysql · try it yourself
SELECT NULL AND 0, NULL OR 1, NULL AND 1, NOT NULL;
+------------+-----------+------------+----------+
| NULL AND 0 | NULL OR 1 | NULL AND 1 | NOT NULL |
+------------+-----------+------------+----------+
|          0 |         1 |       NULL |     NULL |
+------------+-----------+------------+----------+

Test yourself

How does MySQL read a OR b AND c?

  1. (a OR b) AND c
  2. a OR (b AND c)
  3. It is a syntax error
  4. Left to right with no precedence

Which query returns products that are NOT in category 1?

  1. WHERE NOT category_id = 1
  2. WHERE category_id NOT 1
  3. WHERE !category_id = 1 AND
  4. WHERE category_id = NOT 1

What is NULL OR 1?

  1. NULL
  2. 0
  3. 1
  4. An error