Which pattern returns customers whose id matches any customer_id in orders?

Study for the SQL Basics Test. Improve your knowledge with multiple choice questions and detailed explanations. Prepare effectively to master SQL concepts!

Multiple Choice

Which pattern returns customers whose id matches any customer_id in orders?

Explanation:
Membership in a set is the idea being tested. The pattern uses a subquery to build the set of all customer_id values that have orders, and the outer query keeps only those customers whose id is found in that set. This correctly handles any number of matching orders and returns every customer who has at least one order. The other approaches misrepresent how to express that check. Equality with a subquery would fail if there isn’t exactly one matching value, since the subquery could return multiple rows. EXISTS looks for the presence of any row in the subquery, which would be true as long as there is at least one order in the table, all customers would pass or none would pass regardless of their id. A join can produce the same result, but it often requires extra steps like selecting distinct values to avoid duplicates, whereas the membership test with IN cleanly encodes the intended condition.

Membership in a set is the idea being tested. The pattern uses a subquery to build the set of all customer_id values that have orders, and the outer query keeps only those customers whose id is found in that set. This correctly handles any number of matching orders and returns every customer who has at least one order.

The other approaches misrepresent how to express that check. Equality with a subquery would fail if there isn’t exactly one matching value, since the subquery could return multiple rows. EXISTS looks for the presence of any row in the subquery, which would be true as long as there is at least one order in the table, all customers would pass or none would pass regardless of their id. A join can produce the same result, but it often requires extra steps like selecting distinct values to avoid duplicates, whereas the membership test with IN cleanly encodes the intended condition.

Subscribe

Get the latest from Passetra

You can unsubscribe at any time. Read our privacy policy