Monday, April 12, 2010

Write a query to find the Nth largest salary of an employee

Select * From Employee E1 Where
(N-1) = (Select Count(Distinct(E2.Salary)) From Employee E2 Where
E2.Salary > E1.Salary)

for ex: for 3rd largest salary of an employee, the query is:
Select * From Employee E1 Where
2 = (Select Count(Distinct(E2.Salary)) From Employee E2 Where
E2.Salary > E1.Salary)

No comments: