All Exams Test series for 1 year @ ₹349 only
Question

Consider a relation book (title, price) which 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) < 7

The correct answer is

Titles of the seven most expensive books

Understanding the SQL Query for Book Ranking

The question asks us to interpret a specific SQL query applied to a relation (table) named book, which has two attributes: title and price. The query uses a subquery in the WHERE clause to filter the results.

Analyzing the SQL Query Structure

The query is:

SELECT title
FROM book AS B
WHERE (select count (*)
       from book as T
       where T.price > B.price) < 7

Let's break down the components:

  1. SELECT title FROM book AS B: This is the outer query. It selects the title of books from the book table, which is aliased as B for convenience in the WHERE clause.
  2. WHERE (...) < 7: This is the filtering condition for the outer query. It determines which books from table B will have their titles selected. The condition depends on the result of the subquery.
  3. (select count (*) from book as T where T.price > B.price): This is the subquery. For each book B being considered in the outer query, this subquery counts the number of books (aliased as T) in the *same* book table whose price is strictly greater than the price of the current book B.

Interpreting the WHERE Clause Condition

The WHERE clause requires that the count returned by the subquery be less than 7. Let's think about what the subquery's count represents in terms of book ranking by price, assuming no two books have the same price as stated in the question:

  • If the subquery count is 0: There are 0 books with a higher price than book B. This means book B is the most expensive book. $0 < 7$ is true.
  • If the subquery count is 1: There is 1 book with a higher price than book B. This means book B is the second most expensive book. $1 < 7$ is true.
  • If the subquery count is 2: There are 2 books with a higher price than book B. This means book B is the third most expensive book. $2 < 7$ is true.
  • ...
  • If the subquery count is 6: There are 6 books with a higher price than book B. This means book B is the seventh most expensive book. $6 < 7$ is true.
  • If the subquery count is 7: There are 7 books with a higher price than book B. This means book B is the eighth most expensive book. $7 < 7$ is false.

So, the condition (select count (*) from book as T where T.price > B.price) < 7 is true for any book B that has fewer than 7 books more expensive than itself. This includes books with 0, 1, 2, 3, 4, 5, or 6 books with a higher price. These correspond exactly to the 1st, 2nd, 3rd, 4th, 5th, 6th, and 7th most expensive books, respectively.

We can summarize this relationship in a table:

Count of Books with Higher Price Rank of Book B (by price) Is Condition < 7 True?
0 1st (Most Expensive) Yes ($0 < 7$)
1 2nd Most Expensive Yes ($1 < 7$)
2 3rd Most Expensive Yes ($2 < 7$)
3 4th Most Expensive Yes ($3 < 7$)
4 5th Most Expensive Yes ($4 < 7$)
5 6th Most Expensive Yes ($5 < 7$)
6 7th Most Expensive Yes ($6 < 7$)
7 8th Most Expensive No ($7 \not< 7$)

The query selects the titles of all books where the count is 0, 1, 2, 3, 4, 5, or 6. These are precisely the titles of the 1st through 7th most expensive books.

Conclusion

The SQL query lists the titles of all books that are among the top 7 most expensive books. Therefore, it lists the titles of the seven most expensive books.

Revision Table - SQL Query Ranking

Concept Explanation
Outer Query SELECT title FROM book AS B - Selects titles from the book table.
Subquery select count (*) from book as T where T.price > B.price - Counts books with a higher price than the current book B.
WHERE Clause (...) < 7 - Filters for books where the subquery count is less than 7 (i.e., 0 through 6).
Result Titles of books ranked 1st through 7th by price.

Additional Information - SQL Subqueries and Ranking

Subqueries are powerful tools in SQL, allowing you to perform operations that filter or calculate values based on results from another query. In this case, a correlated subquery is used because the inner query (T.price > B.price) refers to the outer query's table alias (B).

This specific technique is a common way to find "top N" or "Nth" ranked items in SQL databases, especially when window functions (like RANK(), DENSE_RANK(), ROW_NUMBER()) are not available or preferred. The condition (select count(*) from table where rank_col > outer.rank_col) < N generally finds the top N items ranked by rank_col in descending order. Similarly, (select count(*) from table where rank_col < outer.rank_col) < N would find the top N items ranked in ascending order.

Understanding correlated subqueries and how they can be used for ranking is crucial for advanced SQL querying.

Was this answer helpful?

Important Questions from SQL

  1. Match the following -

    List IList II
    (a)DDL(i)LOCK TABLE
    (b)DML(ii)COMMIT
    (c)TCL(iii)Natural Difference
    (d)Binary operation(iv)REVOKE
  2. Given the following STUDENT‐COURSE scheme : STUDENT (Rollno, Name, courseno) COURSE (courseno, coursename, capacity), where Rollno is the primary key of relation STUDENT and courseno is the primary key of relation COURSE. Attribute coursename of COURSE takes unique values only. Which of the following query(ies) will find total number of students enrolled in each course, along with its coursename.

    A. SELECT coursename, count(*) 'total' from STUDENT natural join COURSE group by coursename;

    B. SELECT C.coursename, count(*) 'total' from STUDENT S, COURSE C where S.courseno = C.courseno group by coursename;

    C. SELECT coursename, count(*) 'total' from COURSE C where courseno in (SELECT courseno from STUDENT);

  3. Which of the following is used to create a database schema?

  4. Which of the component module of DBMS does rearrangement and possible ordering of operations, eliminate redundancy in query and use efficient algorithms and indexes during the execution of a query?

  5. Which of the following statements are DML statements?

    (a) Update [tablename] Set [columnname] = VALUE

    (b) Delete [tablename]

    (c) Select * from [tablename]

Need Expert Advice?

Start Your Preparation with Prepp Mobile App

Download the app from Google Play & App Store
Download the app from Google Play & App Store
Prepp Mobile App