Thursday, 16 February 2012

How to find Nth highest Salary


Query to Retrieve the Nth Maximum Value


Replace Employee with your table name, and Salary with your column name. Where N is the level of Salary to be determined.
SELECT *
FROM Employee E1
WHERE (N-1) = (
SELECT COUNT(DISTINCT(E2.Salary))
FROM Employee E2
WHERE E2.Salary > E1.Salary)

In the above example, the inner query uses a value of the outer query in its filter condition meaning; the inner query cannot be evaluated before evaluating the outer query. So each row in the outer query is evaluated first and the inner query is run for that row.

No comments:

Post a Comment