tags:

views:

364

answers:

5

Assume that there is a 'Employee' table with salary as one of the columns.

I want to get the 'Top N'th maximum salary alone from the table.

How this can be fetched easily?

A: 
SELECT TOP N employee.name, employee.salary
FROM employee
ORDER BY employee.salary DESC

?

Frederik Gheysels
Top N'th value alone, not all top N!!
Dhana
Then, be clearer on what you want. 'TOP N alone' means -for me- only the TOP N records.When you just want a single record, then be clearer, and say 'the max salary of the 'TOP N'.
Frederik Gheysels
well, n'th means -- only one record. isn't it?
Dhana
A: 
SELECT TOP 1 Salary FROM
(SELECT TOP(N) Salary FROM Employee
ORDER BY Salary DESC) E
Kirtan
+2  A: 
SELECT TOP 1 employee.name, employee.salary from (
    SELECT TOP N employee.name, employee.salary
    FROM employee
    ORDER BY employee.salary DESC )

This gives the Nth from the top.

nils_gate
+1  A: 
WITH    (
        SELECT  e.*, ROW_NUMBER() OVER (ORDER BY employee.salary DESC) AS rn
        FROM    employee
        ) AS q
SELECT  *
FROM    q
WHERE   rn = @n
Quassnoi
I was thinking derived table with ROW_NUMBER(), which is about the same here +1
KM
A: 
 SELECT TOP 1 * FROM Employees
    ORDER BY Employees.Salary DESC
Prashant