Write SQL to find parent entities that have no related child rows.
Answer
Use NOT EXISTS with a correlated subquery or a LEFT JOIN followed by a NULL check on a non-nullable child key. NOT EXISTS is often the clearest anti-join pattern and avoids the NULL semantics trap that can make NOT IN unexpectedly return no rows.
Strong answer includes
- uses NOT EXISTS or a correct LEFT JOIN anti-join
- avoids unsafe NOT IN behavior when NULL may occur
- checks a non-nullable joined child key in the LEFT JOIN form
Practical example
Dialect · Portable SQLRuntime · SQLite sample DB
Runs against the resettable SQLite sample database used by the playground.
SQL
Result
Run the query to see the result.
Use an anti-semi pattern with NOT EXISTS. NOT EXISTS avoids the NULL trap that can make NOT IN evaluate to unknown.
Expected result: Every parent row with zero matching child rows.