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]
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] Correct Answer Titles of the five most expensive books
Query
The outer query selects all titles from book table. For every selected book, the subquery returns count of those books which are more expensive than the selected book.
No two books have the same price
book B :
|
Title |
Price |
|
A |
10 |
|
B |
20 |
|
C |
30 |
|
D |
40 |
|
E |
50 |
|
F |
60 |
|
G |
70 |
|
H |
80 |
book T:
|
Title |
Price |
|
A |
10 |
|
B |
20 |
|
C |
30 |
|
D |
40 |
|
E |
50 |
|
F |
60 |
|
G |
70 |
|
H |
80 |
Query
The where clause of the outer query will be true for the 5 most expensive books.
The outer query selects all titles from the book table. For every selected book, the subquery returns count of those books which are more expensive than the selected book. The where clause of outer query will be true for 5 most expensive book. For example count (*) will be 0 for the most expensive book and count(*) will be 1 for second most expensive book.
Example
count (*) will be 0 for the most expensive book and count(*) will be 1 for the second most expensive book.