Monday, June 13, 2011

Query to find nth highest salary in SQL Server

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)

Thanks to Pinal Dave!!!

1 comment:

  1. This is a nice query. But, understanding the query is tough and you can find a good explanation here:

    http://www.programmerinterview.com/index.php/database-sql/find-nth-highest-salary-sql/

    ReplyDelete