Consider a database table T containing two columns X and Y each of type…

2011

Consider a database table T containing two columns X and Y each of type integer. After the creation of the table, one record (X=1, Y=1) is inserted in the table.

Let MX and MY denote the respective maximum values of X and Y among all records in the table at any point in time. Using MX and MY, new records are inserted in the table 128 times with X and Y values being MX+1, 2*MY+1 respectively. It may be noted that each time after the insertion, values of MX and MY change.

What will be the output of the following SQL query after the steps mentioned above are carried out?

SELECT Y FROM T WHERE X=7;
  1. A.

    127

  2. B.

    255

  3. C.

    129

  4. D.

    257

Attempted by 681 students.

Show answer & explanation

Correct answer: A

Key insight: each new record increases MX by 1 and sets Y = 2*previous_MY + 1.

  • X values generated by the inserts are 2, 3, 4, ..., 1 + number_of_inserts. The record with X = 7 is created on the 6th insert (since initial X = 1).

  • Y follows the recurrence Y_{n} = 2*Y_{n-1} + 1 with initial Y_0 = 1. This solves to Y_{n} = 2^{n+1} - 1 after n inserts.

  • For the record with X = 7 we have n = 6 inserts, so Y = 2^{7} - 1 = 128 - 1 = 127.

Therefore SELECT Y FROM T WHERE X = 7 returns 127.

A video solution is available for this question — log in and enroll to watch it.

Explore the full course: Gate Guidance By Sanchit Sir