EXISTS is a MySQL operator that tests whether a subquery returns at least one row, and NOT EXISTS tests that it returns none. It evaluates to true or false and never looks at the actual column values, so it is the cleanest way to answer "does a related row exist" questions such as customers with orders or customers without any.
This guide shows the syntax, the typical patterns, and why NOT EXISTS is safer than NOT IN when NULL values are possible. The examples work on MySQL 8.4 and later.
Asha has two orders, Ravi and Meera have one each, and Kiran has none.
EXISTS takes a subquery in parentheses. It returns true as soon as the subquery produces one row, whatever that row contains.
The subquery is correlated: it uses c.id from the outer query. The select list inside an EXISTS subquery does not matter, so SELECT 1, SELECT * and SELECT NULL all behave the same. Writing SELECT 1 is the common convention because it makes the intent clear.
Asha is returned once, even though she has two orders. This is a key difference from a join, which would return one row per matching order and need DISTINCT.
NOT EXISTS flips the test and keeps outer rows with no match, which is the classic anti-join.
Use this to find customers who never ordered, products never sold, or users with no profile row.
NOT IN has a trap. If the list returned by the subquery contains even one NULL, the comparison becomes unknown for every row and the query returns nothing.
With a NULL in the list, this returns an empty result instead of Kiran. NOT EXISTS compares rows through a correlated condition, and a NULL there simply fails to match, so the answer stays correct.
| Question | Correct on NULL data | Notes |
|---|---|---|
NOT EXISTS | Yes | Preferred anti-join form |
NOT IN | No, empty result if the list has NULL | Fine only for NOT NULL columns |
LEFT JOIN ... WHERE o.id IS NULL | Yes | Equivalent, more verbose |
The left join version looks like this:
All three forms are usually planned as an anti-join in MySQL 8, so compare them with EXPLAIN instead of guessing which is fastest.
The subquery can hold any filter, which lets you ask richer questions.
This lists customers with at least one order of 100 or more. Combining both operators gives "has a big order but no small one" style logic without any aggregation.
| Approach | Duplicates in result | Handles NULL | Best for |
|---|---|---|---|
EXISTS | No | Yes | Existence checks, big inner tables |
IN (subquery) | No | Careful with NOT IN | Short, readable membership tests |
JOIN | Yes, one per match | Yes | When you also need columns from the other table |
The optimizer rewrites IN and EXISTS subqueries into semijoins where it can, so results and speed are often identical. Choose the form that reads best, and check the plan with EXPLAIN when the query is slow. An index on the inner correlated column, such as orders.customer_id, matters most.
The same test controls which rows change.
This removes customers without orders. In MySQL, deleting from a table while also selecting from that same table in a subquery causes error 1093, but this example reads a different table so it is allowed. Run such statements inside a transaction and preview them with a SELECT first.
No. EXISTS only checks whether any row is returned, so SELECT 1 and SELECT * give the same result. The convention is SELECT 1.
Not always. MySQL 8 often converts both to the same semijoin plan. EXISTS can stop at the first match, so it is a good choice when the inner table is large.
Use NOT EXISTS with a correlated subquery, or a LEFT JOIN with WHERE other.id IS NULL. Avoid NOT IN when the inner column can be NULL.
No. The EXISTS operator returns true or false only, even when the subquery rows contain NULL values.
EXISTS checks whether a subquery returns any row, and NOT EXISTS checks that it returns none. They are correlated subqueries that return each outer row once, ignore the subquery's column values, and handle NULL correctly, which makes NOT EXISTS the safest anti-join. Index the inner join column and confirm the plan with EXPLAIN on large data.