Middle
To select clients from the Customers table who have at least one order in the Orders table, which query will do this correctly? SELECT * FROM Customers WHERE id = (SELECT customer_id FROM Orders); SELECT * FROM Customers c JOIN Orders o ON c.id = o.customer_id; SELECT * FROM Customers, Orders WHERE Customers.id = Orders.id; SELECT * FROM Customers WHERE id IN (SELECT customer_id FROM Orders);
sobes.tech AI
Answer from AI
A correct query to list clients from the Customers table who have at least one order in the Orders table is:
SELECT * FROM Customers WHERE id IN (SELECT customer_id FROM Orders);
Explanation:
- The first option with
=is not suitable because the subquery returns multiple values, while=expects a single value. - The second option with JOIN will return clients with orders but may duplicate clients if they have multiple orders.
- The third option with
WHERE Customers.id = Orders.idis incorrect because it compares different fields (client id and order id).
Using IN with a subquery is a simple and clear way to get all clients who have orders.