MySQL In and Between

IN and BETWEEN let you match a value against a list of options or a range, without stacking up multiple OR conditions.

Matching a List of Values with IN

Suppose you need every order placed from three specific regions: 'North', 'South', or 'West'. Written with OR, that's WHERE region = 'North' OR region = 'South' OR region = 'West' - readable for three values, tedious for ten. The IN operator collapses this into a single, tidy list: WHERE region IN ('North', 'South', 'West'). MySQL evaluates it exactly the same way, but the intent is clearer and the list is easier to maintain.

Orders from three specific regions

SELECT order_id, region, total_amount
FROM orders
WHERE region IN ('North', 'South', 'West');

Matching a Range with BETWEEN

BETWEEN checks whether a value falls within a range, inclusive of both endpoints. WHERE quantity BETWEEN 10 AND 20 is shorthand for WHERE quantity >= 10 AND quantity <= 20 - it matches 10, 20, and everything in between, not just the interior values. BETWEEN works on numbers, dates, and even strings, since MySQL compares them using their natural ordering.

Orders placed during Q1 2025

SELECT order_id, order_date, total_amount
FROM orders
WHERE order_date BETWEEN '2025-01-01' AND '2025-03-31';

Negating with NOT IN and NOT BETWEEN

Both operators can be reversed with NOT. NOT IN (...) matches rows whose value is absent from the list; NOT BETWEEN x AND y matches rows outside the range, on either side. Both keep the same inclusive behavior as their positive form - NOT BETWEEN 40 AND 50 excludes 40 and 50 themselves, since those values belong to the included range.

Employees outside the standard 40-50 age band

SELECT employee_id, first_name, age
FROM employees
WHERE age NOT BETWEEN 40 AND 50;
  • IN accepts a fixed list of literals, or the result of a subquery: WHERE department_id IN (SELECT department_id FROM departments WHERE location = 'Delhi').
  • BETWEEN requires the lower bound first; WHERE price BETWEEN 100 AND 50 with the bounds reversed silently matches nothing - it does not swap them for you.
  • Both operators can use an index efficiently: IN can look up each listed value, and BETWEEN can scan a contiguous range.
  • For date columns that include a time component, BETWEEN '2025-01-01' AND '2025-01-31' can miss rows timestamped later in the day on the 31st - AND order_date < '2025-02-01' is safer for full-day coverage.
TaskUsing ORUsing IN / BETWEEN
Match one of 3 citiescity='Pune' OR city='Agra' OR city='Kochi'city IN ('Pune','Agra','Kochi')
Match ages 18 to 25age>=18 AND age<=25age BETWEEN 18 AND 25
Exclude a list of idsid<>4 AND id<>7 AND id<>9id NOT IN (4,7,9)
Note: IN and BETWEEN aren't limited to WHERE - they work just as well inside a HAVING clause once you're filtering on aggregated results.
Note: NOT IN behaves unexpectedly if the list contains a NULL: WHERE id NOT IN (1, 2, NULL) returns no rows at all, because comparing anything to NULL produces an unknown result rather than true or false. If the list might contain NULLs, filter them out first, or use NOT EXISTS instead.

Exercise: MySQL In and Between

What does the IN operator let you do in a WHERE clause?