← Back to SQL, page 15

sql-0283 · Medium mid

Is EXISTS always faster than IN for subqueries?

Not in modern engines. PostgreSQL usually plans IN (subquery) and EXISTS identically, and SQLite picks between a list lookup and a correlated probe based on indexes. The real differences are semantic: NOT IN breaks when the subquery returns a NULL, while NOT EXISTS does not. Check the plan rather than relying on folklore.

Full answer
sql
CREATE TABLE customers (
  id      INTEGER PRIMARY KEY,
  name    TEXT NOT NULL,
  country TEXT
);
CREATE TABLE orders (
  id          INTEGER PRIMARY KEY,
  customer_id INTEGER REFERENCES customers(id),
  order_date  TEXT NOT NULL,
  status      TEXT NOT NULL
);
CREATE TABLE order_items (
  order_id   INTEGER REFERENCES orders(id),
  product    TEXT NOT NULL,
  qty        INTEGER NOT NULL,
  unit_price INTEGER NOT NULL
);
INSERT INTO customers VALUES (1,'Ana','Spain'),(2,'Bilal','UAE'),(3,'Chloe','France'),(4,'Dev','India');
INSERT INTO orders VALUES
  (101,1,'2026-01-05','shipped'),(102,1,'2026-02-11','shipped'),
  (103,2,'2026-02-14','cancelled'),(104,3,'2026-03-01','shipped'),
  (105,2,'2026-03-09','pending'),(106,NULL,'2026-03-12','shipped');
INSERT INTO order_items VALUES
  (101,'Notebook',2,4),(101,'Pen',5,1),(102,'Bag',1,30),(103,'Pen',10,1),
  (104,'Notebook',3,4),(104,'Bag',1,30),(105,'Lamp',1,25),(106,'Pen',2,1);
sql
EXPLAIN QUERY PLAN SELECT name FROM customers WHERE id IN (SELECT customer_id FROM orders);
text
QUERY PLAN
SEARCH customers USING INTEGER PRIMARY KEY (rowid=?)
LIST SUBQUERY 1
  SCAN orders
sql
EXPLAIN QUERY PLAN SELECT name FROM customers c WHERE EXISTS (SELECT 1 FROM orders o WHERE o.customer_id = c.id);
text
QUERY PLAN
SCAN c
CORRELATED SCALAR SUBQUERY 1
  SCAN o

SQLite plans them differently here. For IN, it reads the orders subquery once into a list and looks up customers by primary key. For EXISTS, it scans customers and runs a correlated probe of orders per row, which is fast only with an index on orders.customer_id. Neither is always better; it depends on table sizes and indexes.

PostgreSQL turns both into the same plan, a hash join against the distinct customer ids from orders:

sql
EXPLAIN (COSTS OFF) SELECT name FROM customers WHERE id IN (SELECT customer_id FROM orders);
text
Hash Join
  Hash Cond: (customers.id = orders.customer_id)
  ->  Seq Scan on customers
  ->  Hash
        ->  HashAggregate
              Group Key: orders.customer_id
              ->  Seq Scan on orders
sql
EXPLAIN (COSTS OFF) SELECT name FROM customers c WHERE EXISTS (SELECT 1 FROM orders o WHERE o.customer_id = c.id);
text
Hash Join
  Hash Cond: (c.id = o.customer_id)
  ->  Seq Scan on customers c
  ->  Hash
        ->  HashAggregate
              Group Key: o.customer_id
              ->  Seq Scan on orders o

The old advice that EXISTS is always faster comes from older optimizers that ran IN subqueries naively.

What does matter:

  • NOT IN with a nullable column returns no rows if any NULL is present (shown in the NULL questions), so prefer NOT EXISTS for anti-joins.
  • IN (1, 2, 3) with literals is a different case: a list, not a subquery.
  • Correlated EXISTS can use an index on the correlated column.

Pick the form that states the intent most clearly, then confirm with EXPLAIN.

sql
CREATE TABLE customers (
  id      INTEGER PRIMARY KEY,
  name    TEXT NOT NULL,
  country TEXT
);
CREATE TABLE orders (
  id          INTEGER PRIMARY KEY,
  customer_id INTEGER REFERENCES customers(id),
  order_date  TEXT NOT NULL,
  status      TEXT NOT NULL
);
CREATE TABLE order_items (
  order_id   INTEGER REFERENCES orders(id),
  product    TEXT NOT NULL,
  qty        INTEGER NOT NULL,
  unit_price INTEGER NOT NULL
);
INSERT INTO customers VALUES (1,'Ana','Spain'),(2,'Bilal','UAE'),(3,'Chloe','France'),(4,'Dev','India');
INSERT INTO orders VALUES
  (101,1,'2026-01-05','shipped'),(102,1,'2026-02-11','shipped'),
  (103,2,'2026-02-14','cancelled'),(104,3,'2026-03-01','shipped'),
  (105,2,'2026-03-09','pending'),(106,NULL,'2026-03-12','shipped');
INSERT INTO order_items VALUES
  (101,'Notebook',2,4),(101,'Pen',5,1),(102,'Bag',1,30),(103,'Pen',10,1),
  (104,'Notebook',3,4),(104,'Bag',1,30),(105,'Lamp',1,25),(106,'Pen',2,1);

EXPLAIN QUERY PLAN SELECT name FROM customers WHERE id IN (SELECT customer_id FROM orders);

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

Checked on SQLite 3.46.1, run twice with identical output. PostgreSQL results in the answer were checked on PostgreSQL 17.12.

Key points

  • PostgreSQL gives IN and EXISTS subqueries the same plan here
  • NOT IN fails with NULLs; NOT EXISTS does not
  • Check plans instead of rules of thumb
  • Indexes on the correlated column matter most

Common mistakes

  • Rewriting every IN as EXISTS for speed
  • Using NOT IN on a nullable column

Follow-ups

  • How does a semi-join differ from an inner join?
  • When can IN with a long literal list be slow?

Tips

  • Separate performance from NULL semantics