GATE / CS ARCHIVE

GATE CSE 2010 / QUESTION 19

Correlated SQL EXISTS filter

Database Management Systems · SQL & Queries · Full GATE CSE 2010 paper

GATE 2010, Q19 1 mark · MCQ
Report issue

SQL & Queries

Question 19, GATE 2010: Schemas: Passenger(pid,pname,age) and Reservation(pid,class,tid).
Passenger rows: (0,Sachin,65), (1,Rahul,66), (2,Sourav,67), (3,Anil,69).
Reservation rows: (0,AC,8200), (1,AC,8201), (2,SC,8201), (5,AC,8203), (1,SC,8204), (3,AC,8202).
Which pids does this query return?
SELECT pid FROM Reservation
WHERE class='AC' AND EXISTS
 (SELECT * FROM Passenger WHERE age>65 AND Passenger.pid=Reservation.pid);
Options: (A) 1,0; (B) 1,2; (C) 1,3; (D) 1,5.
Tap to enlarge
Your answer
Answer & solution

Answer: C (no official key was published)

The outer query first keeps AC reservation rows, whose passenger IDs are 0,1,5,3. The correlated EXISTS requires a Passenger row with the same pid and age greater than 65. ID 0 has age exactly 65 and fails; ID 5 has no Passenger row and fails. IDs 1 and 3 have ages 66 and 69 and pass, giving 1,3, option C.

EXISTS tests whether at least one matching subquery row exists; it does not require every Passenger row to meet the age condition. Passenger 2 is old enough but its only reservation is SC, so the outer AC filter removes it. Passenger 1’s additional SC reservation also does not add another output row. No ORDER BY is present, so 1,3 describes the result members rather than a guaranteed row order.

Original paper, page 4