Skip to main content

GATE CSE 2014Databases › SQLVerified

GATE CSE 2014 PYQ: Consider the following relational schema: employee (empId,empName,empDept) customer…

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');  
  1. A
    Names of all the employees with at least one of their customers having a ‘GOOD’ rating.
  2. B
    Names of all the employees with at most one of their customers having a 'GOOD' rating.
  3. C
    Names of all the employees with none of their customers having a 'GOOD' rating.
  4. D
    Names of all the employees with all their customers having a 'GOOD' rating.
    Correct answer

Answer

(D) Names of all the employees with all their customers having a 'GOOD' rating.

Approach

Evaluate the correlated NOT EXISTS subquery as a universal condition over each employee's customers. The inner query looks for any customer of the current employee whose rating is not 'GOOD'.

The SQL query returns employees for whom there is no customer with rating other than 'GOOD'. Equivalently, it returns employees whose every customer has a 'GOOD' rating. Therefore option D is correct.

Reviewed by GateIQ Editorial Team: IIT & IISc Alumni.