Subqueries
EXISTS vs. IN: Performance and Logic
Senior Data Analyst
February 20, 2026
5 min read
The Filtering Dilemma
I needed to find all customers who had ever placed an order. I could use `IN` or `EXISTS`, but which was better?
The Implementation: Both Approaches
Using IN
SELECT * FROM customers
WHERE id IN (SELECT customer_id FROM orders);
Using EXISTS
SELECT * FROM customers c
WHERE EXISTS (SELECT 1 FROM orders o WHERE o.customer_id = c.id);
Performance Considerations
Pro Tip
Many modern databases optimize both to the same execution plan. When in doubt, use `EXPLAIN ANALYZE` to compare.
Your Task for Today
Rewrite an `IN` subquery as `EXISTS` and compare the performance.
*Day 52: Subqueries in the SELECT Clause.*