// dialect comparison

Excluding rows: NOT IN vs NOT EXISTS vs LEFT JOIN

NOT IN has a NULL trap that silently empties your result.

Filed under Querying

The verdict

NOT EXISTS is the default on all five engines: it is standard, says what you mean, and a NULL in orders.customer_id can't change its result. LEFT JOIN … IS NULL gives the same rows if you test the right side's key, and treat NOT IN over a nullable column as a bug waiting for its first NULL.

Where they differ

Three ways to write "rows with no match": NOT EXISTS (correlated subquery), NOT IN (subquery) and LEFT JOIN … WHERE right.key IS NULL. They return the same rows only while the subquery column holds no NULLs.

How each engine writes it

SQL Server

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

The optimizer plans NOT EXISTS as an anti semi join; NOT IN on a nullable column adds extra NULL checks to the plan.

PostgreSQL

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

Planned as a hash or merge anti join. NOT IN (SELECT …) can't use an anti join and gets slow on big subqueries, besides the NULL trap.

MySQL

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

8.0.17+ converts NOT EXISTS and NOT IN subqueries to antijoins. On older versions LEFT JOIN … WHERE o.id IS NULL was often faster.

SQLite

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

Runs as a correlated lookup per customer, so an index on orders(customer_id) decides the speed.

Oracle

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

Planned as a hash anti join. 11g+ also has a null-aware anti join for NOT IN, which makes it fast, though not NULL-safe.

Questions that come up

Why does NOT IN return no rows at all?

x NOT IN (1, 2, NULL) means x <> 1 AND x <> 2 AND x <> NULL, and the last comparison is unknown, so the whole predicate is never true. One NULL customer_id in orders empties the result. NOT EXISTS only asks whether a matching row exists, so NULLs don't affect it.

When is LEFT JOIN … IS NULL preferable?

Mainly on MySQL before 8.0.17, where it often planned better, or when the query is already built from outer joins. Test a column that can't be NULL in a matched row, such as the right side's primary key (o.id IS NULL). Testing a nullable column also returns customers whose matching orders have NULL there.

How do I make an anti-join fast?

Index the inner table's join column, orders.customer_id here. With that index all three forms probe or hash efficiently on current versions. Compare plans on your own data rather than trusting an old rule of thumb.

Keep reading

// write it once, run it anywhere

Switch engines without relearning SQL

SQLly is a native SQL workbench for macOS, Windows and Linux. Its formatter and plan viewer work in all five dialects.