IN and BETWEEN Operators
What you'll learn: How to write cleaner, more readable SQL queries when filtering by multiple specific values or ranges.
The Problem They Solve
Imagine you need to find all orders from California, Texas, or New York. Without the IN operator, you'd write:
WHERE state = 'CA' OR state = 'TX' OR state = 'NY'
That gets messy fast. Or suppose you want products priced between 50—you'd need:
WHERE price >= 10 AND price <= 50
SQL gives you shortcuts for both scenarios.
The IN Operator
IN lets you specify a list of values to match against. Think of it as "is this value in this set?"
SELECT * FROM orders
WHERE state IN ('CA', 'TX', 'NY');
This is equivalent to multiple OR conditions but far more readable. You can use IN with numbers, strings, or even subquery results (later topic).
The BETWEEN Operator
BETWEEN checks if a value falls within a range, inclusive of both endpoints:
SELECT * FROM products
WHERE price BETWEEN 10 AND 50;
This means "price >= 10 AND price <= 50". It works with numbers, dates, and even strings (alphabetically).
Important: BETWEEN is inclusive. A price of exactly 10 or exactly 50 matches.
Combining Them
You can mix these operators with other WHERE conditions:
SELECT * FROM employees
WHERE department IN ('Sales', 'Marketing')
AND hire_year BETWEEN 2020 AND 2023;
Key Takeaway: Use IN to simplify multiple equality checks and BETWEEN for inclusive range filtering—both make your queries shorter, clearer, and easier to maintain.