MySQL IN and BETWEEN
IN and BETWEEN are shortcuts for common combinations of comparisons. They make conditions shorter and easier to read.
IN: one of a list
col IN (a, b, c) is true when col equals any value in the list. It means the same as col = a OR col = b OR col = c:
SELECT first_name, last_name, country
FROM customers
WHERE country IN ('Spain', 'Italy', 'Portugal');+------------+-----------+----------+
| first_name | last_name | country |
+------------+-----------+----------+
| Ana | Silva | Portugal |
| Elena | Rossi | Italy |
| Lucia | Garcia | Spain |
+------------+-----------+----------+
NOT IN is the opposite:
SELECT order_id, status
FROM orders
WHERE status NOT IN ('delivered', 'cancelled');+----------+------------+
| order_id | status |
+----------+------------+
| 18 | shipped |
| 19 | shipped |
| 20 | processing |
| 21 | processing |
| 22 | processing |
+----------+------------+
IN with a subquery
The list can come from another query. "Customers who placed an order handled by employee 6":
SELECT first_name, last_name
FROM customers
WHERE customer_id IN (SELECT customer_id FROM orders WHERE employee_id = 6)
ORDER BY first_name;+------------+-----------+
| first_name | last_name |
+------------+-----------+
| Ana | Silva |
| Ben | Carter |
| Farah | Khan |
| Hana | Sato |
| Kofi | Mensah |
| Lucia | Garcia |
+------------+-----------+
The NOT IN and NULL trap
If the list contains a NULL, NOT IN never returns true. x NOT IN (1, NULL) means x <> 1 AND x <> NULL, and the second part is unknown:
SELECT 3 NOT IN (1, 2) AS plain, 3 NOT IN (1, 2, NULL) AS with_null;+-------+-----------+
| plain | with_null |
+-------+-----------+
| 1 | NULL |
+-------+-----------+
That bites with subqueries: WHERE id NOT IN (SELECT manager_id FROM employees) returns no rows, because the CEO's manager_id is NULL. Filter the NULLs out (WHERE manager_id IS NOT NULL) or use NOT EXISTS instead.
BETWEEN: a range
col BETWEEN low AND high means col >= low AND col <= high. Both ends are included:
SELECT name, unit_price
FROM products
WHERE unit_price BETWEEN 2.49 AND 2.99
ORDER BY unit_price;+----------------------+------------+
| name | unit_price |
+----------------------+------------+
| Dark Chocolate Bar | 2.49 |
| Cherry Tomatoes 500g | 2.75 |
| Salted Butter 250g | 2.85 |
| Rye Crackers | 2.90 |
| Gala Apples 1kg | 2.99 |
+----------------------+------------+
The products priced exactly 2.49 and 2.99 are in the result. low must be the smaller value: BETWEEN 2.99 AND 2.49 matches nothing.
BETWEEN with dates and text
SELECT order_id, order_date
FROM orders
WHERE order_date BETWEEN '2025-03-01' AND '2025-03-31';
SELECT name FROM categories WHERE name BETWEEN 'B' AND 'D' ORDER BY name;+----------+------------+
| order_id | order_date |
+----------+------------+
| 9 | 2025-03-03 |
| 10 | 2025-03-10 |
| 11 | 2025-03-15 |
| 12 | 2025-03-21 |
+----------+------------+
+-----------+
| name |
+-----------+
| Bakery |
| Beverages |
+-----------+
The text example returned Bakery and Beverages, but not Dairy: 'Dairy' sorts after 'D', since it is longer. For "starts with B or C" a pattern (name < 'D', or LIKE) is clearer.
Note: For
DATETIMEcolumns,BETWEEN '2025-03-01' AND '2025-03-31'misses everything after midnight on March 31 ('2025-03-31'means2025-03-31 00:00:00). Use a half-open range instead:col >= '2025-03-01' AND col < '2025-04-01'.
Test yourself
unit_price BETWEEN 2 AND 3 includes which prices?
- 2 < price < 3
- 2 <= price <= 3
- 2 <= price < 3
- 2 < price <= 3
What is 5 NOT IN (1, 2, NULL)?
- 1 (true)
- 0 (false)
- NULL
- An error
Which is equivalent to country IN ('UK', 'Spain')?
- country = 'UK' AND country = 'Spain'
- country = 'UK' OR country = 'Spain'
- country BETWEEN 'UK' AND 'Spain'
- country LIKE 'UK%Spain'