Avinash Narnaware
Avinash Narnaware

Reputation: 43

Need help with SQL Server query

I want write this query in SQL Server

from (
    select DISTINCT salary 
    from employee 
    order by salary desc
) 
where rownum = 3;

Upvotes: 3

Views: 75

Answers (2)

D'Arcy Rittich
D'Arcy Rittich

Reputation: 171559

See ROW_NUMBER():

E.g.,

WITH EmployeeSalary AS
(
    select salary, 
        ROW_NUMBER() OVER (order by salary desc) AS 'RowNumber'
    FROM employee 
    group by salary --you can't use DISTINCT because ROW_NUMBER() makes each row distinct
) 
SELECT * 
FROM EmployeeSalary 
WHERE RowNumber = 3;

Upvotes: 7

SELECT DISTINCT salary 
FROM employee 
WHERE rownum = 3 
ORDER BY salary 

The caps are optional. Is rownum a column in employee or are you looking for only the third row returned?

Upvotes: 0

Related Questions