Find 1st, 2nd, 3rd, 4th, nth topmost salary from an Employee table

Many time  interviewer ask “How to find Nth Highest Salary of Employee”. Here is a query for this

The following solution is for getting 2nd highest salary from Employee table

SELECT TOP 1 salary
FROM (
SELECT DISTINCT TOP 2 salary
FROM employee
ORDER BY salary DESC) Emp
ORDER BY salary


 You can change and use it for getting 3rd  highest salary from Employee table as follows

SELECT TOP 1 salary
FROM (
SELECT DISTINCT TOP 3 salary
FROM employee
ORDER BY salary DESC) Emp
ORDER BY salary

so you can find nth highest salary also using same query.
Ebook Download
View all
Learn
View all