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

  • **IN**: The subquery runs once and creates a list. Better when the subquery result is small.
  • **EXISTS**: The subquery runs for each row in the outer query but stops as soon as one match is found. Better when the subquery result is large.
  • 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.*

    Ready to put your knowledge into practice?

    Join SQL Mastery and learn through interactive exercises.