Sub topic 6
Using IN with a Subquery
This chapter assumes you can write basic SELECT statements, understand tables and columns, use comparison operators, and combine conditions with AND and OR. You will learn how to use a subquery with IN when the values to match come from another table.
Why use `IN` with a subquery?
IN tests whether a value matches any value in a list. For example:
SELECT id, name
FROM employees
WHERE department_id IN (2, 5, 7);This is equivalent to several equality checks joined with OR:
SELECT id, name
FROM employees
WHERE department_id = 2
OR department_id = 5
OR department_id = 7;A subquery lets the database produce that list dynamically. A subquery is a query inside another query. With IN, the subquery must return exactly one column, but it can return any number of rows.
Suppose employees.department_id refers to departments.id. To find employees in departments located in Berlin, query the department table inside IN:
SELECT e.id, e.name
FROM employees AS e
WHERE e.department_id IN (
SELECT d.id
FROM departments AS d
WHERE d.location = 'Berlin'
);The inner query first produces the IDs of Berlin departments. The outer query then keeps employees whose department_id matches one of those IDs.
How the two queries fit together
The subquery compares values from the related table. The outer query returns the rows you want to display.
The subquery is usually called the inner query. The query containing it is the outer query. The database optimizer may execute them in an order different from the written order, but the logical result is the same.
| Part | Example | Purpose |
|---|---|---|
| Outer expression | `e.department_id` | Value being tested |
| `IN` | `IN (...)` | Tests membership |
| Subquery result | `SELECT d.id ...` | Supplies possible values |
Column and type requirements
The expression before IN and the subquery's selected column should represent the same kind of value. This is valid when department_id and departments.id use compatible numeric types:
WHERE e.department_id IN (
SELECT d.id
FROM departments AS d
);This is usually an error in design, even if a database converts the values:
WHERE e.department_id IN (
SELECT d.name
FROM departments AS d
);The outer expression is an ID, while the subquery returns names. Select the column that represents the relationship.
`IN` versus a join
A join is often another way to express the same filter:
SELECT e.id, e.name
FROM employees AS e
JOIN departments AS d
ON d.id = e.department_id
WHERE d.location = 'Berlin';Use either form when both clearly express the requirement. IN emphasizes membership in a set of values. A join is useful when you also need columns from the related table. If a department can match multiple rows in a more complex join, the join can duplicate outer rows, while IN returns each matching outer row once.
NULL and subquery results
NULL means an unknown or missing value. If the subquery returns NULL, an unmatched outer value can produce an unknown result rather than TRUE. The WHERE clause keeps only rows whose condition is TRUE.
For reliable membership tests, select a non-null key when possible. If you need the opposite condition, be especially careful with NOT IN: a NULL anywhere in its subquery result can prevent expected rows from matching. NOT EXISTS is often safer for “no related row” tests.
Exercise
Write a query that returns the IDs and names of employees whose department is located in Paris. Use an IN subquery.
Show answer
SELECT e.id, e.name FROM employees AS e WHERE e.department_id IN ( SELECT d.id FROM departments AS d WHERE d.location = 'Paris' );