# EXISTS & Correlated Subqueries

> Ask "is there at least one matching row?" with EXISTS, understand subqueries that use the outer row, and fix NOT IN with NOT EXISTS.

Subject: SQL | Lesson 24 of 44 | 6 min read
Interactive lesson: https://codewithanimation.in/sql/exists-and-correlated-subqueries
Previous: [Subqueries](https://codewithanimation.in/sql/subqueries.md) | Next: [Common Table Expressions (WITH)](https://codewithanimation.in/sql/common-table-expressions.md)

## EXISTS asks: is there any?

`EXISTS (subquery)` is true when the subquery returns at least one row, and false when it returns none. It never returns NULL. The values in the subquery's select list do not matter, so people write `SELECT 1`.

A **correlated** subquery uses a column of the outer query. The inner query is then run once for each outer row, with that row's value plugged in.

## Has one, or has none

"Which customers have at least one ticket?" and "which customers have none?" are the most common questions of this kind. EXISTS and NOT EXISTS answer both directly, and they cope with NULL better than IN and NOT IN.

## Customers with a ticket

```sql
SELECT name
FROM customers
WHERE EXISTS (
  SELECT 1 FROM tickets
  WHERE tickets.customer_id = customers.id
);
```

For Asha (id 1), the inner query finds 2 tickets, so `EXISTS` is true. For Ben, 1 ticket, true. For Chen, Dara and Eli it finds none, so false. The result is Asha and Ben.

Add `NOT` to find the customers with no ticket: `WHERE NOT EXISTS (...)` returns Chen, Dara and Eli.

## Common mistake: Why NOT EXISTS beats NOT IN

One ticket has a NULL `customer_id`. `NOT IN (SELECT customer_id FROM tickets)` then returns no rows at all. `NOT EXISTS` returns the correct three customers, because a NULL never matches any customer, and EXISTS is never unknown.

Correlated subqueries run once per outer row in your mental model, which is fine for learning. The database is free to find a faster plan that gives the same answer, but a missing index can still make a correlated subquery slow on big tables.

Forgetting the link between the two queries is a common bug. Without `tickets.customer_id = customers.id`, the inner query returns the same rows for every customer, so EXISTS is true for everyone, or for no one.

## Tip: For "has none", reach for NOT EXISTS

Use `EXISTS` for "has at least one" and `NOT EXISTS` for "has none". Keep `IN` for short, fixed lists and for subqueries where you know there is no NULL.

## Try it: Same question, four tests

Switch between EXISTS, NOT EXISTS, IN and NOT IN, and read the badge for each customer. Then turn on Only Billing tickets and watch NOT IN change.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/sql/exists-and-correlated-subqueries

## Check yourself

### Question 1

`WHERE EXISTS (SELECT 1 FROM tickets WHERE tickets.customer_id = customers.id)` is evaluated for Chen, who has no tickets. What is the result?

- A. FALSE
- B. NULL
- C. TRUE, because the subquery ran

Correct answer: A

No ticket matches Chen's id, so the subquery returns no rows and EXISTS is false.

- Why not B: EXISTS never returns NULL. It is true or false depending on whether any row is found.
- Why not C: EXISTS is true only when the subquery returns at least one row. Here it returns none.

### Question 2

Which query correctly lists the customers with no tickets, even though one ticket has a NULL customer_id?

- A. WHERE NOT EXISTS (SELECT 1 FROM tickets WHERE tickets.customer_id = customers.id)
- B. WHERE id NOT IN (SELECT customer_id FROM tickets)
- C. WHERE id = (SELECT customer_id FROM tickets)

Correct answer: A

NOT EXISTS only asks whether a matching row exists, and a NULL never matches, so the answer stays true or false.

- Why not B: The NULL in the list makes every test UNKNOWN, so this returns no rows.
- Why not C: That is a scalar subquery that returns several rows, which is an error.

### Question 3

What makes a subquery correlated?

- A. It refers to a column of the outer query
- B. It uses EXISTS
- C. It returns more than one column

Correct answer: A

A correlated subquery depends on the current outer row, so its result can change from row to row.

- Why not B: EXISTS can be used with or without a link to the outer query. The link is what makes it correlated.
- Why not C: The number of columns has nothing to do with it.

## Remember this

- EXISTS is true if the subquery returns at least one row, false if none. It is never NULL.
- A correlated subquery refers to the outer row, so it is evaluated for each outer row.
- NOT EXISTS is safe with NULL values. NOT IN is not.
- Always link the inner query to the outer one, or EXISTS gives the same answer for every row.
