4,600 views
1 1 vote

 

Relation schema is:-

suppliers(sid,sname,addr)

parts(pid,pname,color)

catalog(sid,pid,cost)

 

please expalin what does the above query do?

specially how this inner query working?

1 Answer

0 0 votes

The given query is Co-related Nested query and it's execution order it TOP-BOTTOM-TOP.

Query is

SELECT C.sid
FROM Catalog C
WHERE NOT EXISTS (SELECT P.pid
                  FROM Parts P
                  WHERE P.color='Red'
                  AND (NOT EXISTS (
                  SELECT C1.sid
                  FROM Catalog C1
                  WHERE C1.sid=C.sid 
                  AND
                  C1.pid=P.pid
                  )))

Here we have 2 cases

Case 1: No red Color part is available: In this case 

                             SELECT P.pid FROM Parts P WHERE P.color='Red'

Would return an empty row Set and the "AND" condition after it would fail, so inner query result set would be empty always and hence NOT exists true always and hence all suppliers from the Catalog relation would be listed.

Case 2: Red Color part is available:

SELECT C.sid
FROM Catalog C

 Select a tuple from Catalog C, and take it's sid(C.sid)

             SELECT P.pid
                  FROM Parts P
                  WHERE P.color='Red' .........(1)
   
                  AND
                (NOT EXISTS
                   (
                  SELECT C1.sid
                  FROM Catalog C1
                  WHERE C1.sid=C.sid 
                  AND
                  C1.pid=P.pid ...................(2)
                  )))

    Condition (1) would select all RED color parts, with their part ID's

    Condition (2) will take the Sid from the outermost query (C.sid) and for this supplier, it will check whether he has supplied a Red color part, and if he does, he will be enlisted by condition (2).

But NOT EXISTS is applied on condition (2) and this will cause to select only those Red color part and their part Id's, which this supplier(C.sid) has not supplied.

Consider there are three red color parts (P1,P2,P3) and there are two suppliers S1 which supplies P1,P2 and Supplier S2 suppliers P1,P2,P3.

For Supplier S1,

Condition (1) would enlist P1,P2,P3

Condition (2) along with NOT EXISTS would result in FALSE for Part P1 and P2 , but true for P3.

so Condition 1 AND 2 would result in tuple with part Id P3 and this is a RED color part not Supplied by Supplier S1.

Since NOT Exists is present in the outermost query, this supplier S1, would be rejected from the result set.

For Supplier S2,

Condition (1) would enlist P1,P2,P3

Condition (2) along with NOT EXISTS would be false for All parts P1,P2,P3 and hence

Condition (1) AND Condition (2) would be false and result in an empty result set, resulting in setting NOT EXISTS clause in the outer query to be TRUE, and Supplier S2 would be enlisted in result set.

Hence, the given query selects all Suppliers who supply every red Part provided Red parts were Available.

Position:
Show:

Related questions

0 0 votes
0 0 answers
1.6k
1.6k views
Aman Janko asked Mar 11, 2018
1,622 views
Given schema:$Suppliers$ ($sid:$$ integer$, $sname:$ $string$, $address:$ $string$)$Parts$ ($pid:$ $integer$, $pname:$ $string$, $color:$ $string$)$Catalog$ ( $sid:$ $int...
0 0 votes
1 1 answer
2
2 views
GO Classes asked 44 minutes ago
2 views
A disk has $200$ tracks numbered $0$ through $199$.The disk head is currently at track $:184$Disk requests arrive for:$184,\ 187,\ 176,\ 182,\ 199$If SSTF scheduling is u...
0 0 votes
1 1 answer
2
2 views
GO Classes asked 46 minutes ago
2 views
Suppose a disk repeatedly services requests for one particular track while requests for other tracks remain unserved. This phenomenon is called disk-arm sticking.Which of...
0 0 votes
1 1 answer
4
4 views
GO Classes asked 1 hour ago
4 views
Which disk scheduling algorithm selects the pending request requiring the smallest movement of the disk arm from its current position, thereby choosing the minimum seek t...