Practice mode DBMS and SQL

Table Customer(id) has rows 1, 2, 3. Table Orders(cust_id) has rows 1, 1, 3. SELECT COUNT(*) FROM Customer c WHERE NOT EXISTS (SELECT 1 FROM Orders o WHERE o.cust_id = c.id); What is the result?

Correct answer C. 1

Explanation

EXISTS is true if the subquery returns at least one row; what it selects (here the constant 1) does not matter. For each customer the correlated subquery looks for that customer's orders. Customers 1 and 3 have orders, so NOT EXISTS is false for them. Only customer 2 has none, giving a count of 1. This is the standard pattern for finding rows with no match.

Report a problem

Answer checked How we write and check questions

Tip: press A, B, C or D to answer, and N for the next question.

Advertisement

Have a question about this answer?

Your email address will not be published. Required fields are marked *

Links and website addresses are not allowed in comments.