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

EXISTS and NOT EXISTS

Part of the SQL Roadmap

In short

Often you don't care how many related rows there are, only whether any exist. EXISTS answers that yes-or-no question cleanly, and NOT EXISTS is the safest way to ask 'has none'.

EXISTS returns true if a subquery finds at least one row, and NOT EXISTS if it finds none. It is the safest way to check for related rows and, unlike NOT IN, is not broken by NULLs.

The jaadu holds a parcel in a housing society corridor, pressing one doorbell as a door opens a crack, while the next door stays firmly shut with a dusty doormat.

Think of it like this

Ringing the doorbell

Delivering a parcel in a Delhi housing society, you ring the doorbell. You don't need to count everyone at home. The moment one person opens the door, you know someone is in. EXISTS stops at the first matching row and returns true. NOT EXISTS is ringing and hearing nothing at all.

Watch it happen

NOT IN vs NOT EXISTS when a NULL sneaks in

NOT IN (subquery)

  • Compares the value with every item in the list
  • x <> NULL is unknown, never true
  • One NULL in the list means zero rows returned
  • Fails silently with no error

NOT EXISTS (subquery)

  • Asks only: did the subquery find any row?
  • Answer is always true or false
  • NULLs in the other table don't break it
  • Can stop at the first match, so it's efficient

Use EXISTS for 'has at least one' and NOT EXISTS for 'has none'. Avoid NOT IN unless the column can never be NULL.

The big ideas

What you need to know

True or false only

EXISTS doesn't return data, just whether the subquery found at least one row.

Stops early

The database can stop searching at the first match. That's why it suits 'any' questions.

Usually correlated

The subquery links to the outer row, like oi.product_id = p.product_id. Without that link, the answer is the same for every row.

SELECT 1 is fine

What you select inside EXISTS doesn't matter. SELECT 1 is a common, readable choice.

See it in code

Walk through the code

sql
1-- Customers with at least one order above ₹5,0002SELECT c.customer_id, c.name3FROM customers AS c4WHERE EXISTS (5  SELECT 16  FROM orders AS o7  WHERE o.customer_id = c.customer_id8    AND o.amount > 50009);10 11-- Products that have never been ordered12SELECT p.product_id, p.name, p.category13FROM products AS p14WHERE NOT EXISTS (15  SELECT 116  FROM order_items AS oi17  WHERE oi.product_id = p.product_id18);

1/3For each customer, look for any order of theirs above ₹5,000. One is enough.

A semi join and an anti join. Both work the same in PostgreSQL, MySQL and SQL Server.

Save yourself the pain

Common mistakes

Oops

WHERE EXISTS (SELECT 1 FROM orders WHERE amount > 5000)

Do this instead

With no link to the outer row, it's true for every customer if any big order exists. Add o.customer_id = c.customer_id.

Oops

WHERE product_id NOT IN (SELECT product_id FROM order_items)

Do this instead

If any product_id there is NULL, you get no rows. NOT EXISTS gives the right answer.

Oops

Writing SELECT * inside EXISTS 'to be safe'.

Do this instead

It makes no difference to the result. SELECT 1 just makes the intent clearer.

Your mission

Yes-or-no questions

~20 min

Quick check

The subquery in WHERE id NOT IN (subquery) returns 3, 8, NULL. What does the outer query return?

Go deeper

  1. 1Find customers who have placed at least one order using EXISTS.
  2. 2Find products that have never been ordered using NOT EXISTS.
  3. 3Compare NOT EXISTS with NOT IN when the subquery contains a NULL.

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