Skip to content
Official PYQ · 2005

GATE CS 2005 Q77 — Tuple Calculus and SQL

Explain with AI

The relation book (title, price) contains the titles and prices of different books. Assuming that no two books have the same price, what does the following SQL query list?

select title
from book as B
where (select count(*)
    from book as T
    where T.price>B.price) < 5
Choose one option
View worked solution

Answer: D

To understand what the SQL query returns, let's analyze the correlated subquery:

select count(*)
from book as T
where T.price > B.price

For any book BB in the outer query, this subquery counts how many books TT have a price strictly greater than the price of BB. Let this count be CC.

The outer query selects the title of book BB if and only if C<5C < 5. Since no two books have the same price, we can rank the books by price in descending order:

  • 1st most expensive book: There are 00 books more expensive than it (C=0C = 0). Since 0<50 < 5, it is selected.
  • 2nd most expensive book: There is 11 book more expensive than it (C=1C = 1). Since 1<51 < 5, it is selected.
  • 3rd most expensive book: There are 22 books more expensive than it (C=2C = 2). Since 2<52 < 5, it is selected.
  • 4th most expensive book: There are 33 books more expensive than it (C=3C = 3). Since 3<53 < 5, it is selected.
  • 5th most expensive book: There are 44 books more expensive than it (C=4C = 4). Since 4<54 < 5, it is selected.
  • 6th most expensive book: There are 55 books more expensive than it (C=5C = 5). Since 5≮55 \not< 5, it is not selected.

Thus, the query selects the titles of the five most expensive books.

Free practice · Your answers on this page are not saved.

Published
Report a correction
Keep exploringMore Tuple Calculus and SQL questions