Question

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');
Choose one option
Official answer and solution
Official answer
D
From
Official GATE 2014 answer key

Solution

Examine the NOT EXISTS condition in the correlated subquery to identify the qualifying employees.

  1. Analyze the Subquery:

    SELECT custId
    FROM customer C
    WHERE C.salesRepId = E.empId
    AND C.rating <> 'GOOD'
    

    For a given employee EE, this inner query selects any customer handled by EE whose rating is NOT 'GOOD'.

  2. Analyze WHERE NOT EXISTS:

    • NOT EXISTS returns TRUE if the subquery returns an empty set of rows.
    • The subquery returns zero rows if and only if employee EE has NO customer whose rating is different from 'GOOD'.
  3. Combine with Given Precondition:

    • The problem specifies that "each employee makes a sale to at least one customer".
    • Since every employee has at least one customer, and none of their customers have a rating other than 'GOOD', every single customer belonging to employee EE must have a 'GOOD' rating.
  4. Therefore, the query returns the names of all employees with all their customers having a 'GOOD' rating (option D).

Final answer

D

Common trap

Confusing 'none of their customers have a rating <> GOOD' (all are GOOD) with 'none of their customers are GOOD' (option C).

The answer is the one in the official GATE answer key. The solution is this site's own explanation.

Source

Paper
GATE 2014 Computer Science and Information Technology (CS) Session 3
Question
64 of 65, printed as CS Q.54
Section
Computer Science and Information Technology

Report an error in this question

GATE questions are the work of the organising institutes. This site is independent and is not affiliated with them.