Sub topic 3
Filtering with IN and Multi-Row Subqueries
Assume you know basic SELECT, WHERE, JOIN, aggregate functions, and NULL behavior. This chapter focuses on subqueries that return multiple rows.
A subquery is a query nested inside another query. A multi-row subquery returns a list of values, usually from one column. The outer query can test whether a value belongs to that list.
Using `IN` with a subquery
IN returns true when a value matches at least one value in a set.
Suppose these tables exist:
| Table | Important columns |
|---|---|
| `customers` | `id`, `name` |
| `orders` | `customer_id`, `total` |
To find customers who have placed an order larger than 100, first identify the matching customer IDs. Then use those IDs to filter customers:
SELECT name
FROM customers
WHERE id IN (
SELECT customer_id
FROM orders
WHERE total > 100
);The inner query might return:
| `customer_id` |
|---|
| 2 |
| 5 |
| 8 |
The outer query then returns customers whose id is 2, 5, or 8. The subquery can return one row, many rows, or no rows. IN handles all three cases.
The subquery must return one column because id is being compared with one value at a time. The data types should also be compatible. Comparing an integer customer ID with a text description is usually an error or produces an unwanted conversion.
Understanding the evaluation
Conceptually, the database evaluates the query in two stages:
The database may optimize the operation rather than literally running the inner query first. The important model is that the inner query supplies a set of candidate values.
You can also use IN with a subquery that groups data:
SELECT name
FROM customers
WHERE id IN (
SELECT customer_id
FROM orders
GROUP BY customer_id
HAVING SUM(total) >= 500
);Here, the inner query returns customers whose combined orders total at least 500. HAVING filters groups after GROUP BY.
`IN` compared with a join
The same question can often be written with a join:
SELECT DISTINCT c.name
FROM customers AS c
JOIN orders AS o ON o.customer_id = c.id
WHERE o.total > 100;The join can produce several rows for one customer when that customer has several qualifying orders. DISTINCT removes duplicates. The IN version naturally expresses the question “does this customer ID appear in the qualifying set?” The join version is often better when you also need columns from the matching orders.
| Use this form | When it fits |
|---|---|
| `IN` with a subquery | You need to test membership in a result set |
| `JOIN` | You need columns from both tables or detailed matching rows |
For example, this is unsafe if several customers have orders over 100:
SELECT name
FROM customers
WHERE id = (
SELECT customer_id
FROM orders
WHERE total > 100
);Replace = with IN unless you have guaranteed that the inner query returns at most one row.
Exercise
Write a query that returns the names of customers who have at least one order with a total of 1,000 or more.
Show answer
SELECT name FROM customers WHERE id IN ( SELECT customer_id FROM orders WHERE total >= 1000 );