0

How to get 7th highest salary from table in SQL Server?

I tried this:

SELECT max(salary)
FROM emptable
WHERE salary < (SELECT max(salary)
                FROM emptable);

3 Answers 3

4
SELECT salary
FROM   (
            SELECT salary
            ,      DENSE_RANK()OVER(ORDER BY salary) DR
            FROM emptable
       ) T
WHERE  T.DR = 7
1

From SQL Server 2012 and onwards:

SELECT DISTINCT salary
FROM emptable
ORDER BY salary
OFFSET 6 ROWS
FETCH NEXT 1 ROWS ONLY;
0

You can as the below:

SELECT MAX(salary) 
FROM (SELECT DISTINCT TOP 7 salary FROM emptable ORDER BY salary) A

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.