Sub topic 5
Testing Membership with `IN`
Assume you know basic SELECT statements, tables, columns, comparison operators, and AND and OR. This chapter shows how to test whether a column matches one value from a set of allowed values.
Replacing Repeated `OR` Conditions
The IN predicate checks whether an expression equals any value in a list. A predicate is a condition that can be true, false, or unknown.
SELECT id, name, department
FROM employees
WHERE department IN ('Sales', 'Marketing', 'Support');This query is equivalent to:
SELECT id, name, department
FROM employees
WHERE department = 'Sales'
OR department = 'Marketing'
OR department = 'Support';IN is usually easier to read and maintain. Add or remove an allowed value in one place instead of editing several OR conditions.
The values in the list must be compatible with the column being tested. Text values need quotes. Numeric values do not.
SELECT id, name
FROM employees
WHERE id IN (101, 205, 309);The list can contain expressions, but each expression should produce a value that can be compared with the left-hand expression.
Understanding the Result
SQL evaluates each row separately. For every employee, the database checks whether department matches at least one string in the list.
IN does not mean that a column contains several values. It tests one column value against several candidate values. For example, a department column normally contains one department per row.
You can negate the test with NOT IN:
SELECT id, name, department
FROM employees
WHERE department NOT IN ('Sales', 'Marketing', 'Support');This keeps rows whose department is not any of the listed values. NOT IN has an important interaction with NULL, which is a marker for a missing or unknown value.
Using `IN` with a Subquery
An IN list is useful when the allowed values are known in the query. When the values come from another table, use a subquery. A subquery is a query nested inside another query.
SELECT id, name
FROM employees
WHERE department_id IN (
SELECT id
FROM departments
WHERE location = 'Berlin'
);The inner query first produces the IDs of departments located in Berlin. The outer query keeps employees whose department_id appears in that result.
The subquery must return one column for this form of IN. It may return many rows.
This query is often clearer than joining when the question is simply “does a matching value exist?” A join may be more appropriate when you also need columns from departments.
SELECT e.id, e.name, d.name AS department_name
FROM employees AS e
JOIN departments AS d
ON d.id = e.department_id
WHERE d.location = 'Berlin';Both queries can express the same filter. The database optimizer may transform them into similar execution plans, but you should still choose the form that best communicates your intent.
Practical Checks
- Use
INfor a short, fixed set of alternatives. - Use a subquery when another table supplies the set.
- Ensure both sides use compatible data types.
- Check whether the tested column or subquery result can contain
NULL. - Do not generate an empty
IN ()list in application code. Many databases reject that syntax. Handle an empty input set before constructing the query.
Exercise
Rewrite WHERE status = 'new' OR status = 'processing' OR status = 'shipped' with IN. What happens to a row where status is NULL?
Show answer
Use WHERE status IN ('new', 'processing', 'shipped'). A NULL status produces an unknown comparison result, so the row is filtered out.