Consider the following relation schema: Product(id, name, price) What will be the output of the following SQL code? SELECT max(price) FROM Product WHERE price < (SELECT max(price) FROM Product);
Consider the following relation schema: Product(id, name, price) What will be the output of the following SQL code? SELECT max(price) FROM Product WHERE price < (SELECT max(price) FROM Product); Correct Answer Second highest price of a product
The correct answer is option 1.
Concept:
Order or SQL query evaluation:
- From: Cartesian product of tables.
- Where: Select the rows.
- Group by: Divide the rows into groups.
- Having: Select the groups.
- Expressions: Expressions In select are evaluated.
- Distinct: Distinct in select (Eliminate the duplicates).
- Ser-Operations: (Union, intersect, Except)
- Order By: Sort the rows.
Relation schema:
Product(id, name, price)
| id | name | price |
| 1 | Ram | 100 |
| 2 | Ravi | 120 |
| 3 | Raju | 90 |
| 4 | Richard | 105 |
Query:
SELECT max(price)
FROM Product
WHERE price < (SELECT max(price) FROM Product);
Step 1:
Choose the product table from the database.
Step 2:
Go to the conditions in where clause, price < (SELECT max(price) FROM Product);
Step 3:
Execute the inner query, and select the product table from the database.
Step 4:
Evaluate the expressions, and select the max price from the database. And gives output to the outer query.
Asper example max price is 120.
Step 5:
The outer query compares the next max value(second max value) of the price with the output inner query.
The output of the query second-highest price of a product satisfies these conditions (max price < inner query max price).
As per the example, it compares like,
Max value of price in the outer query, 120<120 is false.
The next max value of price in the outer query 105<120 is True. Here 105 is the second-highest price of the product.
Hence the correct answer is the second-highest price of a product.