What is the simplest way to get the nth highest value from a result set using plain SQL?
The result set would be huge, thus need to consider performance too.>
Let's say you want to find the 5th highest salary, you first take the first 5 distinct salary order by descending order so that the last one is the 5th highest salary (See inner query). You then reorder it in ascending order and take the first.
SELECT TOP 1 Salary FROM
(
SELECT DISTINCT TOP 5 Salary
FROM Employee
ORDER BY Salary DESC) t
ORDER BY Salary ASC