How to find Nth highest salary in ms sql server
some guys confused how to write a query for getting output in sql server.
there are so many way to get Nth salary .......... i am explaining two ways below find :
select * from (select Dense_Rank() over (order by salary desc) as denserank,salary from SalaryMaster) as r where denserank=Value
Value=on which position rank you want i.e. 2 or 5th
this is a Dense_Rank() function which gives you a sequence without skipping a number order. this is the best way to get Nth salary in a query.
--------------------------------------------------------------------
2nd way.
with CTE
with CTESalary
AS
(
Dense_Rank() over (order by salary desc)as denserank,
,salary from SalaryMaster
)
select * from CTESalary where denserank=2
Comments
Post a Comment