16 Apr 2017

Query to find nth highest salary or Second highest salary in SQL Server



Query to find nth highest salary or Second highest salary in SQL Server

 Select * from Employee

select Max(salary) from Employee where Salary < (select Max(Salary) from Employee)
=========================================================================
WITH RESULT AS
(
Select Salary , DENSE_RANK() over (Order By Salary DESC) as DENSERANK from employee
)

Select top 1 Salary From RESULT where DENSERANK = 2 

you can change your DENSERANK value accordingly in where condition
=========================================================================

Select TOP 1 Salary From (select TOP 2 Salary from employee order By Salary Desc) Result Order by Salary




No comments:

Post a Comment