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

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'
);

Let n be the number of comparisons performed when the above SQL query is optimally executed. If linear search is used to locate a tuple in a relation using primary key, then n lies in the range

Answer: B. 44 - 48Simple aur proper explanation: Comparison CalculationWe calculate the comparisons step by step. 1. First inner query: Red cars SELECT R.did FROM Cars C,…

  1. A.

    36 - 40

  2. B.

    44 - 48

  3. C.

    60 - 64

  4. D.

    100 - 104

Attempted by 25 students.

Show answer & explanation

Correct answer: B

Simple aur proper explanation:

Comparison Calculation

We calculate the comparisons step by step.

1. First inner query: Red cars

SELECT R.did
FROM Cars C, Reserves R
WHERE R.cid = C.cid AND C.color = 'red'
  • First, we scan all 4 tuples of the Cars relation to find red cars.
    Comparisons = 4

  • There are 2 red cars. For each red car, all 10 tuples of Reserves are checked to match R.cid = C.cid.
    Comparisons = 2 × 10 = 20

So, for the first inner query:

4 + 20 = 24 comparisons

The resulting did values are:

{22, 22, 31, 31, 64}

After removing duplicates:

{22, 31, 64}

2. Second inner query: Green cars

SELECT R.did
FROM Cars C, Reserves R
WHERE R.cid = C.cid AND C.color = 'green'
  • Scan all 4 tuples of Cars to find green cars.
    Comparisons = 4

  • There is 1 green car, so all 10 tuples of Reserves are checked.
    Comparisons = 1 × 10 = 10

So, for the second inner query:

4 + 10 = 14 comparisons

The resulting did values are:

{22, 31, 74}

3. Intersection of the two sets

First set:

{22, 31, 64}

Second set:

{22, 31, 74}

Common values are:

{22, 31}

Using linear search:

  • For 22, it matches on the first comparison → 1 comparison

  • For 31, it matches on the second comparison → 2 comparisons

  • For 74, all 3 values of the first set are checked → 3 comparisons

So:

1 + 2 + 3 = 6 comparisons

4. Finding these drivers in the Drivers table

Now we need to find did = 22 and did = 31 in the Drivers table using linear search.

  • 22 is found on the first try → 1 comparison

  • 31 is found on the third try → 3 comparisons

So:

1 + 3 = 4 comparisons

Total Comparisons

First inner query = 24
Second inner query = 14
Intersection = 6
Drivers table search = 4

24+14+6+4=48 {24 + 14 + 6 + 4 = 48}

Therefore, the total number of comparisons is 48, so Option B is the correct answer.

Explore the full course: Gate Guidance By Sanchit Sir

Loading lesson…