Stop 07 · Subqueries, CTEs & Set OperationsTopics · 2/9

Subqueries with IN

Part of the SQL Roadmap

In short

"Which customers bought something from the Ayurveda range this month?" You need a list first, then a filter on that list. A subquery inside IN does both in one statement.

A subquery inside IN returns a list of values to filter against, such as customers whose IDs appear in this month's orders.

The jaadu in an airline cap stands at an airport boarding gate holding a short paper list, waving one traveller through while gently stopping another with a raised hand.

Think of it like this

The boarding gate list

At the boarding gate in Chennai airport, the agent doesn't re-check everyone's booking. The check-in counter has already sent a list of seat numbers that checked in. Each passenger in the queue is let through only if their number is on that list. The inner query is the check-in counter making the list; the outer query is the gate agent checking each row against it.

Watch it happen

How WHERE ... IN (subquery) runs

outer query
inner query
ID list

who bought Ayurveda?

1/5The outer query needs a list before it can filter customers.

The big ideas

What you need to know

Inner returns one column

The subquery inside IN must return exactly one column. That column becomes the list.

No duplicate rows

A customer who appears five times in the list still appears once in the result. A join would repeat them.

Test the inside first

Run the inner query on its own and look at the list before wrapping it in IN.

Careful with NOT IN

If the list contains a NULL, NOT IN returns nothing. Use NOT EXISTS for the 'not in' version.

See it in code

Walk through the code

sql
1SELECT c.customer_id, c.name, c.city2FROM customers AS c3WHERE c.customer_id IN (4  SELECT o.customer_id5  FROM orders AS o6  JOIN order_items AS oi ON oi.order_id   = o.order_id7  JOIN products    AS p  ON p.product_id  = oi.product_id8  WHERE p.category = 'Ayurveda'9    AND o.order_date >= DATE '2026-11-01'10)11ORDER BY c.name;

1/3The inner query builds a list of customer IDs who bought Ayurveda products in November.

Customers who bought from the Ayurveda category this month, each listed once. Works the same in PostgreSQL, MySQL and SQL Server.

Save yourself the pain

Common mistakes

Oops

WHERE customer_id IN (SELECT customer_id, name FROM ...)

Do this instead

IN compares one value to a list, so the subquery must return one column.

Oops

Rewriting it as a plain JOIN and getting each customer several times.

Do this instead

A join repeats the customer for every matching item. Use IN, EXISTS or SELECT DISTINCT.

Oops

Using NOT IN (subquery) to find customers who didn't buy.

Do this instead

One NULL in the list breaks it completely. Use NOT EXISTS.

Your mission

Lists inside filters

~20 min

Quick check

The inner query returns customer IDs 5, 5, 5, 9. How many times does customer 5 appear in the outer result?

Go deeper

  1. 1Find customers who bought from a specific category using IN with a subquery.
  2. 2Rewrite it as a join and compare readability.
  3. 3Check for duplicates, which IN handles but a join may not.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge