2nd Highest Point

   

Write an SQL query to fetch the second highest point from the table.



Input :-

Games table :-
ID is the Primary Key column for the Games table.

ID

Points

1

200

2

150

3

450

4

300

5

250


Output :-

SecondHighestPoint

300



Query :-
select max(Points) as SecondHighestPoint 
from Games where Points < 
(select max(Points) as SecondHighestPoint 
from Games order by Points desc);

Explanation : - 

The given query is used to find the second highest value of the ‘Points’ column in the "Games" table.

Let's break down the query step by step:

  • The inner subquery (select max(Points) as SecondHighestPoint from Games order by Points desc) retrieves the maximum value of the 'Points' column from the "Games" table. The order by Points desc clause ensures that the maximum value is selected.
  • The outer query select max(Points) as SecondHighestPoint from Games where Points < (inner subquery) is then executed. It selects the maximum value from the 'Points' column, but only considers the values that are less than the maximum value obtained from the inner subquery.
  • The result is assigned the alias 'SecondHighestPoint' using the as keyword.

By comparing the "Points" column values to the maximum value obtained from the inner subquery, the outer query effectively filters out the highest value and retrieves the next highest value, which gives us the second highest point in the "Games" table.






No comments

darkmode