Problem Statement
When comparing EXISTS and IN, which choice is generally safer with NULLs and scalable for large subqueries?
Explanation
EXISTS stops at the first matching row and is not affected by NULLs in the subquery. It works well with an index on the correlated predicate.
IN compares a value to a set. If the subquery can yield NULLs, NOT IN can return no rows due to three-valued logic. Deduplicate the subquery or prefer NOT EXISTS.
Code Solution
SolutionRead Only
SELECT * FROM orders o WHERE EXISTS ( SELECT 1 FROM order_items i WHERE i.order_id = o.id );
