Consider the following relational schema:
employee (empId,empName,empDept)
customer (custId,custName,salesRepId,rating)
salesRepId is a foreign key referring to empId of the employee relation. Assume that each employee makes a sale to at least one customer. What does the following query return?
SELECT empName FROM employee E
WHERE NOT EXISTS (SELECT custId
FROM customer C
WHERE C.salesRepId = E.empId
AND C.rating <> 'GOOD'); View worked solution
Answer: D
To understand what the SQL query returns, let's analyze the inner subquery and the outer NOT EXISTS condition step-by-step:
- The Inner Subquery:
For a given employee , this subquery retrieves the IDs of all customers assigned to whose rating is notSELECT custId FROM customer C WHERE C.salesRepId = E.empId AND C.rating <> 'GOOD''GOOD'. - The Outer Query with
NOT EXISTS: The outer query selects the name of employee only if the subquery returns an empty set (i.e.,NOT EXISTSevaluates toTRUE). - Conclusion:
If an employee has no customers with a rating other than
'GOOD', it logically means that all of their customers must have a rating of'GOOD'.
The problem states: "Assume that each employee makes a sale to at least one customer." This assumption ensures that the set of customers for any employee is non-empty, meaning the query will indeed return employees where all of their customers have a 'GOOD' rating.
Option Analysis:
- Option A: "At least one..." would correspond to an
EXISTSquery withrating = 'GOOD'. - Option B: "At most one..." would require counting the customers.
- Option C: "None of their customers..." would correspond to
NOT EXISTSwithrating = 'GOOD'. - Option D: Correctly matches our logical deduction.
Free practice · Your answers on this page are not saved.
