Reputation: 164
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);
Upvotes: 0
Views: 77
Reputation: 15150
SELECT salary
FROM (
SELECT salary
, DENSE_RANK()OVER(ORDER BY salary) DR
FROM emptable
) T
WHERE T.DR = 7
Upvotes: 4
Reputation: 4092
You can as the below:
SELECT MAX(salary)
FROM (SELECT DISTINCT TOP 7 salary FROM emptable ORDER BY salary) A
Upvotes: 0
Reputation: 522817
From SQL Server 2012 and onwards:
SELECT DISTINCT salary
FROM emptable
ORDER BY salary
OFFSET 6 ROWS
FETCH NEXT 1 ROWS ONLY;
Upvotes: 1