To Fetch 3rd highest or Nth highest salary(or any number) from a table.
One way is tou use subquery like this:
select Min(Salary) from salary where salary in
(
select distinct top 3 salary from salary order by salary desc)
*********************************************
Other way is to use dense rank this:
select top 1 * from
(
select salary,rn=dense_rank() over (order by salary desc)
from salary
) as a
where a.rn=3
These queries are for third highest salary, if you want your desired highest salary then replace TOP 3 with
TOP (desired number) from above query.
And Replace Where a.rn=3 with Where a.rn=(Desired number) from 2nd query.
select Min(Salary) from salary where salary in
(
select distinct top 3 salary from salary order by salary desc)
*********************************************
Other way is to use dense rank this:
select top 1 * from
(
select salary,rn=dense_rank() over (order by salary desc)
from salary
) as a
where a.rn=3
These queries are for third highest salary, if you want your desired highest salary then replace TOP 3 with
TOP (desired number) from above query.
And Replace Where a.rn=3 with Where a.rn=(Desired number) from 2nd query.
Comments
Post a Comment