GATE CS 2009 · SQL Queries

MCQ · 2 marks · Problem DBMS-SQL-0112

Consider the following relational schema:Suppliers(sid:integer, sname:string, city:string, street:string)Parts(pid:integer, pname:string, color:string)Catalog(sid:integer, pid:integer, cost:real)Consider the following relational query on the above database:
SELECT S.sname
FROM Suppliers S
WHERE S.sid NOT IN (SELECT C.sid
                    FROM Catalog C
                    WHERE C.pid NOT IN (SELECT P.pid
                                        FROM Parts P
                                        WHERE P.color <> 'blue'))
Assume that relations corresponding to the above schema are not empty. Which one of the following is the correct interpretation of the above query?
  1. A
    Find the names of all suppliers who have not supplied any blue part.
  2. B
    Find the names of all suppliers who have not supplied a non-blue part.
  3. C
    Find the names of all suppliers who have supplied only blue parts.
  4. D
    Find the names of all suppliers who have not supplied only blue parts.

Solve it on GATE DA practice →More Relational Model & SQL problems

Show the worked solution\[\boxed{\text{inside out: non-blue parts} \to \text{suppliers of blue parts} \to \text{the other suppliers}}\]The innermost query gives the non-blue parts. "C.pid NOT IN" them keeps the Catalog rows for blue parts, so the middle query gives the suppliers who supplied some blue part. "S.sid NOT IN" that set leaves the suppliers who supplied no blue part (including suppliers with no Catalog rows).(B) would keep only suppliers of blue parts (or of nothing), (C) needs every part to be blue, and (D) includes suppliers of both colours — all incorrect.(The official GATE 2009 key was A, whose original wording, "who have supplied a non-blue part", is not exactly what the query does; option A is reworded here to the exact meaning.)Answer: A

GATE DA practice: past exam problems by chapter, with worked solutions · gatedaprep.com