Problem Statement
You must return customers who have at least one order. Which pattern is most robust and avoids duplicate inflation?
Explanation
EXISTS is a semi-join. It returns true on the first match and stops. It does not duplicate the left row, so you avoid DISTINCT.
IN can work if the subquery is deduplicated and non-null. EXISTS is safer and often produces efficient plans with good indexes.
Code Solution
SolutionRead Only
SELECT c.customer_id FROM customers c WHERE EXISTS ( SELECT 1 FROM orders o WHERE o.customer_id = c.customer_id );
Practice Sets
This question appears in the following practice sets:
