3

I want write this query in SQL Server

from (
    select DISTINCT salary 
    from employee 
    order by salary desc
) 
where rownum = 3;
1
  • 6
    So.. what is your question? Are gremlins stopping you from writing the query? Commented May 13, 2011 at 15:20

2 Answers 2

7

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;
Sign up to request clarification or add additional context in comments.

Comments

0
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?

2 Comments

If you post code, XML or data samples, please highlight those lines in the text editor and click on the "code samples" button ( { } ) on the editor toolbar to nicely format and syntax highlight it!
Thanks EvoD , rownum is not column. I m lokking for 3rd row

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.