How To get 2nd (second) highest value Or How to get 2nd highest salary using sql server.
There are different ways to get second(2nd) highest value in sql server .
Approach 1:
Get the highest value from the table and then neglect it using 'not in' clause .In below example we are getting the second highest marks in the class.
SELECT TOP1 marks FROM Tblresult WHERE marks NOT IN
(SELECT TOP 1 marks FROM Tblresult ORDER BY marks DESC)
ORDER BY marks DESC
Approach 2:
Get the 3rd(third) highest marks:
SELECT TOP 1 MarksFROM
(SELECT
DISTINCT TOP 2
Marks
FROM Tblresult
ORDER BY
Marks
DESC) aORDER BY
Marks
Approach 3:
Get the nth Highest value in sql Server:
SELECT TOP 1 MarksFROM
(SELECT
DISTINCT TOP n
Marks
FROM Tblresult
ORDER BY
Marks
DESC) aORDER BY
Marks
where n is the number of value you want to skip .suppose you want to get 10th highest value than put 10 in the place of n.
No comments:
Post a Comment