EXISTS, IN and JOIN

One question written three ways. Two give the same answer, one does not, and NULL decides which.

EXISTS, IN and JOIN

One question, four ways

query.sql SQLite
Result

Worth knowing

EXISTS asks whether any row matches and stops at the first one. It never returns the row itself.
IN compares a value against a list. With a NULL in that list, NOT IN can never be true.
JOIN multiplies rows: one customer with three orders becomes three rows unless you aggregate or add DISTINCT.
Modern planners often compile all three to the same plan. Correctness is the reason to choose, not speed.

Overview

Suppose you need customers who have placed at least one order. Three common approaches are a join, an IN subquery, and EXISTS. The right choice depends first on the result you need: matching pairs, membership, or just whether a match exists.

In this guide
  1. Use JOIN when you need columns from both sides
  2. Use EXISTS for a yes/no match
  3. The NULL trap in NOT IN
  4. Choose by multiplicity
  5. Further reading

Use JOIN when you need columns from both sides

A join combines matching rows. If one customer has five orders, joining customers to orders returns five rows for that customer. That multiplication is correct when the result is one row per customer-order pair. It becomes an accidental duplicate if you only wanted a list of customers.

query.sqlSQLite
Result

DISTINCT can remove repeated customer rows, but it may hide a result-shape mistake and add work.

Use EXISTS for a yes/no match

EXISTS tests whether its subquery returns at least one row. The outer row appears once, regardless of how many matching orders exist.

query.sqlSQLite
Result

For positive membership, IN (subquery) is also clear when the subquery produces the candidate values. The optimizer may transform these forms similarly; do not assume one is always faster. Compare plans on real data.

The NULL trap in NOT IN

If the subquery used by NOT IN contains a NULL, a nonmatching value may evaluate to UNKNOWN rather than TRUE. WHERE keeps only TRUE rows, so expected results disappear. NOT EXISTS often expresses “there is no matching row” more safely, but write the correlation condition carefully and account for nullable keys.

This follows SQL's three-valued logic; see NULL handling. For filtering before grouping, see WHERE.

Choose by multiplicity

To show every matching order, use JOIN because each result should represent a customer-order pair. To list a customer once if any order qualifies, use EXISTS because it tests membership without multiplying the outer row. IN is concise when the subquery is a list of IDs. These are semantic choices; modern optimizers may turn them into similar plans. A useful test is to add two matching orders for one customer: if the answer should still contain that customer once, the query should express existence or explicitly deduplicate.

Further reading

Check yourself

0 of 3

Answer without scrolling back up.

  1. Why does NOT IN return no rows when the subquery contains a NULL?

  2. Why does a JOIN sometimes return a customer twice when EXISTS does not?

  3. Why is `SELECT 1` conventional inside EXISTS?

Cheat sheet

EXISTS, IN and JOIN

Write the same question with EXISTS, IN and JOIN, compare the execution plans, and find the version that quietly returns wrong rows.

DATABASE · vizlearn.in/database/exists_vs_in_vs_join.html

About the author

Ashish Jangra builds and maintains VizLearn. Every module here is written and the visualisation behind it hand-built, so the numbers in a readout come from the same code that draws the picture. Corrections are genuinely welcome and get priority over everything else — if a page states something wrong, or an animation misrepresents what the algorithm does, get in touch.

Teaching a course with this? Individual teachers can use it freely; for a whole department or programme, see institutional licences.