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
Titles of the seven most expensive books
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.
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:
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:
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.
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.
| 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. |
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.
Match the following -
| List I | List II | ||
| (a) | DDL | (i) | LOCK TABLE |
| (b) | DML | (ii) | COMMIT |
| (c) | TCL | (iii) | Natural Difference |
| (d) | Binary operation | (iv) | REVOKE |
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);
Which of the following is used to create a database schema?
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?
Which of the following statements are DML statements?
(a) Update [tablename] Set [columnname] = VALUE
(b) Delete [tablename]
(c) Select * from [tablename]