How to find fifth highest salary in a single query in SQL Server
These work in SQL Server 2000
DECLARE @result int
SELECT TOP 5 @result = Salary FROM Employees ORDER BY Salary DESC
Syntax should be close. I can't test it at the moment.
Or you could go with a subquery:
SELECT MIN(Salary) FROM (
SELECT TOP 5 Salary FROM Employees ORDER BY Salary DESC
) AS TopFive
Again, not positive if the syntax is exactly right, but the approach works.
You can try some thing like :
select salary
from Employees a
where 5=(select count(distinct salary)
from Employees b
where a.salary > b.salary)
order by salary desc
The below query to gets the Highest salary after particular Employee name.
Just have a look into that!
SELECT TOP 1 salary FROM (
SELECT DISTINCT min(salary) salary
FROM emp where salary > (select salary from emp where empname = 'John Hell')
) a
ORDER BY salary
SELECT TOP 1 salary
FROM (
SELECT DISTINCT TOP n salary
FROM employee
ORDER BY salary DESC) a
ORDER BY salary
where n > 1 -- (n is always greater than one)
You can find any number of highest salary using this query.
SELECT MIN(Salary) FROM (
SELECT TOP 2 Salary FROM empa ORDER BY Salary DESC
) AS TopFive
It's working correctly, please use it.
To find the 5th higest salary from a database, the query is..
select MIN(esal) from (
select top 5 esal from tbemp order by esal desc) as sal
its working check it out