Consider a database with three relation instances shown below. The primary…

2006

Consider a database with three relation instances shown below. The primary keys for the Drivers and Cars relation are did and cid respectively and the records are stored in ascending order of these primary keys as given in the tables. No indexing is available in the database.

2006_84_852006_84_85_22006_84_85_3

What is the output of the following SQL query?

select D.dname
from Drivers D
where D.did in  (
                        select R.did
                        from Cars C, Reserves R
                        where R.cid = C.cid and C.colour = 'red'
                        intersect
                        select R.did
                        from Cars C, Reserves R
                        where R.cid  = C.cid and C.colour = 'green'
                         )

  1. A.

    Karthikeyan, Boris

  2. B.

    Sachin, Salman

  3. C.

    Karthikeyan, Boris, Sachin

  4. D.

    Schumacher, Senna

Attempted by 72 students.

Show answer & explanation

Correct answer: A

Goal: find drivers who have reserved at least one red car and at least one green car (the intersection of drivers who reserved red cars and drivers who reserved green cars).

  1. Identify car IDs by colour: red cars have cid 102 and 104; green cars have cid 103.

  2. Find drivers who reserved red cars by scanning Reserves for cid 102 or 104: dids found are 22, 31, and 64 (Karthikeyan, Boris, Sachin).

  3. Find drivers who reserved green cars by scanning Reserves for cid 103: dids found are 22, 31, and 74 (Karthikeyan, Boris, Senna).

  4. Compute the intersection of the two driver sets: {22, 31, 64} ∩ {22, 31, 74} = {22, 31}.

  5. Map the resulting dids to driver names: did 22 = Karthikeyan, did 31 = Boris.

Final output: Karthikeyan, Boris

Explore the full course: Gate Guidance By Sanchit Sir