MySQL WHERE
The WHERE clause filters rows: only rows for which its condition is true make it into the result. It comes right after FROM:
SELECT column1, column2, ...
FROM table_name
WHERE condition;SELECT first_name, last_name, city
FROM customers
WHERE country = 'UK';+------------+-----------+------------+
| first_name | last_name | city |
+------------+-----------+------------+
| Ben | Carter | London |
| Farah | Khan | Manchester |
| George | Brown | London |
+------------+-----------+------------+
Text and numbers
Text values go in single quotes; numbers don't. Comparisons on text are case-insensitive with MySQL's default collation, so 'uk' finds 'UK' too:
SELECT name, unit_price
FROM products
WHERE unit_price > 10;
SELECT COUNT(*) AS uk_customers
FROM customers
WHERE country = 'uk';+--------------------+------------+
| name | unit_price |
+--------------------+------------+
| Espresso Beans 1kg | 24.50 |
| Chocolate Cake | 18.00 |
| Laundry Pods (30) | 11.90 |
+--------------------+------------+
+--------------+
| uk_customers |
+--------------+
| 3 |
+--------------+
Comparison operators
| Operator | Meaning | Example |
|---|---|---|
= |
equal | status = 'shipped' |
<> or != |
not equal | status <> 'cancelled' |
> < |
greater / less than | unit_price > 5 |
>= <= |
greater / less or equal | units_in_stock <= 10 |
BETWEEN a AND b |
inside a range (inclusive) | unit_price BETWEEN 2 AND 3 |
LIKE |
matches a pattern | name LIKE 'S%' |
IN (...) |
equals one of a list | country IN ('UK', 'Spain') |
IS NULL |
has no value | email IS NULL |
BETWEEN, LIKE, IN and IS NULL each get their own lesson.
Dates
Dates are written as strings in 'YYYY-MM-DD' form, and compare in calendar order:
SELECT order_id, order_date, status
FROM orders
WHERE order_date >= '2025-05-01';+----------+------------+------------+
| order_id | order_date | status |
+----------+------------+------------+
| 17 | 2025-05-03 | delivered |
| 18 | 2025-05-12 | shipped |
| 19 | 2025-05-20 | shipped |
| 20 | 2025-05-28 | processing |
| 21 | 2025-06-02 | processing |
| 22 | 2025-06-03 | processing |
+----------+------------+------------+
Conditions are expressions
A condition is just an expression that evaluates to 1 (true), 0 (false) or NULL (unknown). You can see that by selecting it:
SELECT name, units_in_stock, units_in_stock = 0 AS sold_out
FROM products
WHERE category_id = 1;+----------------------+----------------+----------+
| name | units_in_stock | sold_out |
+----------------------+----------------+----------+
| Espresso Beans 1kg | 40 | 0 |
| Earl Grey Tea | 85 | 0 |
| Orange Juice 1L | 60 | 0 |
| Sparkling Water 6x1L | 0 | 1 |
+----------------------+----------------+----------+
Only rows where the condition is exactly true pass the filter. A comparison with NULL is never true, which is why WHERE email = NULL finds nothing; you need IS NULL (see the NULL Values lesson).
WHERE in UPDATE and DELETE
The same clause decides which rows UPDATE and DELETE touch. That makes it the most important safety feature in SQL: an UPDATE without WHERE changes every row.
UPDATE products SET units_in_stock = 50 WHERE product_id = 4;
SELECT name, units_in_stock FROM products WHERE product_id = 4;+----------------------+----------------+
| name | units_in_stock |
+----------------------+----------------+
| Sparkling Water 6x1L | 50 |
+----------------------+----------------+
Note: If a query matches no rows at all, the
mysqlclient prints nothing in script mode (interactively it saysEmpty set). So an empty output in a playground means "zero rows", not an error.
Test yourself
Which condition finds orders that are not cancelled?
- status != cancelled
- status <> 'cancelled'
- status NOT 'cancelled'
- status =! 'cancelled'
With MySQL's default collation, does WHERE country = 'uk' match the value 'UK'?
- Yes, comparison is case-insensitive
- No, it is case-sensitive
- Only on Windows
- Only with LIKE
What does SELECT 5 > 3; return?
- TRUE
- 1
- 'yes'
- 5