shuffleet
Home
Practice
Problems
Lessons
Plans
Compete
Challenges
Interviews
Leaderboard
Progress
Skills
Analytics
Submissions
Account
Settings

EXISTS and NOT EXISTS

Junior · 3 min read

In two sentences

EXISTS keeps a row when at least one matching row exists somewhere else; NOT EXISTS keeps it when none does. Unlike a join, they never multiply rows and never bring columns along: they only answer yes or no for each row.

See it

customer_id
c1
c2
c3
order_idcustomer_id
1c1
2c1
3c2
4NULL ← guest checkout
questionqueryresult
customers with ordersJOIN ordersc1, c1, c2
customers with ordersWHERE EXISTS (…)c1, c2
customers without ordersWHERE NOT EXISTS (…)c3
customers without ordersLEFT JOIN … WHERE o.order_id IS NULLc3
customers without ordersWHERE customer_id NOT IN (SELECT customer_id FROM orders)nothing

The join returns c1 twice because c1 has two orders. NOT IN returns nothing because of the guest order's NULL.

EXISTS answers yes or no per customer; the join repeats c1; NOT IN breaks on a NULL

In SQL

Customers who ordered (a semi-join):

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

Customers who never ordered (an anti-join):

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

Orders pointing at a customer that doesn't exist: a referential-integrity check.

SQL
SELECT o.*
FROM orders o
WHERE o.customer_id IS NOT NULL
  AND NOT EXISTS (SELECT 1 FROM customers c WHERE c.customer_id = o.customer_id);

Insert only the rows that aren't there yet, so a re-run adds nothing:

SQL
INSERT INTO dim_customer (customer_id, name)
SELECT s.customer_id, s.name
FROM staging_customers s
WHERE NOT EXISTS (SELECT 1 FROM dim_customer d WHERE d.customer_id = s.customer_id);

Common mistakes

  • NOT IN over a column that can be NULL. One NULL in the subquery makes every comparison unknown, and the query returns nothing. Use NOT EXISTS. See NULLs and missing values.
  • Using JOIN for "has at least one". The join repeats the row once per match; then someone adds DISTINCT to hide it. EXISTS says what you mean.
  • Forgetting the correlation. EXISTS (SELECT 1 FROM orders) without WHERE o.customer_id = c.customer_id is true for every customer as soon as orders has a row.
  • LEFT JOIN … IS NULL on the wrong column. Test a column that is never NULL on a matching row (the key you joined on, or a primary key), not one that can be NULL anyway.

What an interviewer asks next

  • "NOT IN, NOT EXISTS or LEFT JOIN … IS NULL?" NOT EXISTS: correct with NULLs, and engines run it as an anti-join. LEFT JOIN … IS NULL is also correct; NOT IN is the trap.
  • "Make this insert safe to re-run." INSERT … SELECT … WHERE NOT EXISTS, or a MERGE.

Go further

PostgreSQL's page on subquery expressions spells out exactly when IN and NOT IN return NULL; Trino behaves the same way.

Practise it

Find Orphaned Orders Before They Reach the Dashboard, then The Churn Report Returns Zero Rows: NULL Semantics for the NOT IN trap, then Facts Before Their Dimension: Inferred Members.