Find nth highest salary in sql server, oracle and mysql
Suppose we have table Employee has columns EmpName ,address, salary
Sql Server
Example1
Select TOP 1 Salary as '5th Highest Salary'
From (SELECT DISTINCT TOP 5 Salary from Employee ORDER BY Salary DESC)
a ORDER BY Salary ASC
Example2
SELECT * FROM tblEmplyee Emp1 WHERE (5) = (
SELECT COUNT (DISTINCT (Emp2.Salary))
FROM tblEmplyee Emp2
WHERE Emp2.Salary >= Emp1.Salary
)
Oracle
select * from ( select salary ,dense_rank() over (order by salary desc) ranking from tblEmployee ) where ranking = 5
My_Sql
SELECT * FROM tblEmplyee emp1 WHERE (5 ) = ( SELECT COUNT(Emp2.salary )
FROM tblEmplyee Emp2 WHERE Emp2.salary >= emp1.salary )
Linq
var employee = Employees .OrderByDescending(e => e.Salary) .Skip(4) .First();
No comments:
Post a Comment