01 / 22
?

EXISTS vs. IN
SQL Subqueries

Master two essential SQL operators for filtering with subqueries: understand when to use each, how they differ in execution, and which one to reach for in every scenario.

SQL

What Are Subqueries?

A subquery is a query nested inside another query. It lets you ask multi-step questions in a single SQL statement: "Show me all customers who have placed at least one order."

Outer Query
The main SELECT that produces your final result set. It references the subquery for filtering.
Inner Query
The nested SELECT inside parentheses. It supplies data or a condition to the outer query.

The IN Operator

IN checks whether a value matches any value in a list. That list can be hard-coded or produced by a subquery.

SELECT name FROM customers WHERE customer_id IN ( SELECT customer_id FROM orders );

The EXISTS Operator

EXISTS checks whether a correlated subquery returns at least one row. It doesn't care about what data comes back: just whether something exists.

SELECT name FROM customers c WHERE EXISTS ( SELECT 1 FROM orders o WHERE o.customer_id = c.customer_id );

How IN Executes

Step 1 Run Inner Query Result List {101, 102, 105} materialized in memory Step 2 Check Each Row customers table id=101 Alice 101 IN {101,102,105} → ✓ id=103 Carol 103 IN {101,102,105} → ✕ id=105 Eve 105 IN {101,102,105} → ✓ id=107 Grace 107 IN {101,102,105} → ✕

How EXISTS Executes

Outer Query Pick a row from customers Correlated Subquery Any order for this customer? repeat Per-row execution Row: Alice (id=101) → SELECT 1 FROM orders WHERE cust=101 → Found 1 row → ✓ (stop early) Row: Carol (id=103) → SELECT 1 FROM orders WHERE cust=103 → Found 0 rows → ✕ Row: Eve (id=105) → SELECT 1 FROM orders WHERE cust=105 → Found 1 row → ✓ (stop early) Row: Grace (id=107) → SELECT 1 FROM orders WHERE cust=107 → Found 0 rows → ✕

The Core Rule

IN
Compares a single column value against a materialized list of values returned by the subquery.
EXISTS
Tests for the existence of rows in a correlated subquery. Returns TRUE/FALSE: the actual data is irrelevant.

Correlated vs. Non-Correlated

EXISTS almost always uses a correlated subquery: the inner query references a column from the outer query. IN typically uses a non-correlated (independent) subquery.

-- Non-correlated (IN): inner runs once independently WHERE id IN (SELECT id FROM other_table) -- Correlated (EXISTS): inner references outer's alias WHERE EXISTS ( SELECT 1 FROM other_table o WHERE o.id = main.id -- references outer! )

Key Distinction

IN EXISTS
Returns A list of values TRUE or FALSE
Subquery type Non-correlated (usually) Correlated (typically)
NULLs in subquery Can cause issues No problem
Short-circuits? No (builds full list) Yes (stops at first match)
Best when inner result Is small Is large (with index)

The NULL Trap with IN

If the subquery for IN returns any NULL values, the result of NOT IN can become unexpected. This is one of the most common SQL bugs.

-- Suppose orders.customer_id has a NULL row SELECT name FROM customers WHERE customer_id NOT IN ( SELECT customer_id FROM orders ); -- Returns ZERO rows! (not what you expect)

Performance Considerations

IN Wins When
The subquery returns a small number of rows. The entire list fits easily in memory and the comparison is fast.
EXISTS Wins When
The inner table is large but has an index on the correlated column. It stops after the first match.

How to Choose

1
Check for NULLs. If the subquery column can be NULL and you're using NOT IN, switch to NOT EXISTS immediately.
2
Consider the result size. Small, fixed list? Use IN. Large table with an index? Use EXISTS.
3
Need multiple column checks? EXISTS handles complex conditions in the WHERE clause. IN only compares one expression.
4
Readability. For a simple value list (e.g., WHERE status IN ('active', 'pending')), IN is cleaner and more readable.

Example 1: Simple IN

Goal: Find all students enrolled in either "Math" or "Science" classes.

SELECT student_name FROM students WHERE class_id IN ( SELECT id FROM classes WHERE subject IN ('Math', 'Science') );

Example 2: EXISTS with Correlation

Goal: Find all products that have been ordered at least once in the last 30 days.

SELECT product_name, price FROM products p WHERE EXISTS ( SELECT 1 FROM order_items oi JOIN orders o ON o.id = oi.order_id WHERE oi.product_id = p.id AND o.order_date >= CURRENT_DATE - 30 );

Example 3: NOT EXISTS is Safer

Goal: Find customers who have never placed an order. The orders table has some rows where customer_id is NULL.

-- DANGEROUS: NOT IN with NULLs SELECT name FROM customers WHERE id NOT IN ( SELECT customer_id FROM orders ); -- Returns EMPTY SET! -- SAFE: NOT EXISTS ignores NULLs SELECT name FROM customers c WHERE NOT EXISTS ( SELECT 1 FROM orders o WHERE o.customer_id = c.id ); -- Returns correct results!

Common Mistakes

✕ NOT IN with NULLs
Using NOT IN when the subquery column can contain NULL. The result silently becomes empty.
✕ SELECT * in EXISTS
Writing SELECT * inside EXISTS. It works but is misleading: EXISTS only checks row existence. Use SELECT 1.
✕ Missing Correlation
Forgetting the WHERE clause that links the inner and outer queries. The subquery's result is the same for every outer row, returning all rows or no rows.

Summary Comparison

Feature IN EXISTS
Syntax simplicity Simpler Requires alias + correlation
NULL safety Risky with NOT IN Always safe
Multi-column checks Not supported Supported
Hard-coded list Natural fit Not applicable
Large inner tables Slower (full materialization) Faster (short-circuit)
Optimizer rewrite Modern databases often treat them equivalently

Question 1 of 5

What does the EXISTS operator return?

Question 2 of 5

You write WHERE id NOT IN (SELECT parent_id FROM items) and the subquery returns {10, 20, NULL}. What happens?

Question 3 of 5

Which SQL operator is typically paired with a correlated subquery?

Question 4 of 5

When is IN generally more appropriate than EXISTS?

Question 5 of 5

Both queries return the same result. Which statement about them is true?