Subash B
Subash B

Reputation: 164

How to get any highest salary from SQL Server table

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

Answers (3)

HoneyBadger
HoneyBadger

Reputation: 15150

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

Upvotes: 4

neer
neer

Reputation: 4092

You can as the below:

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

Upvotes: 0

Tim Biegeleisen
Tim Biegeleisen

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

Related Questions