Welcome

Hello, Welcome to my blog. If you like feel free to refer others

Friday 7 October 2011

Nth Highest Salary Using SQL Query

Here is the SQL Query:
SELECT * FROM emptable e1 WHERE (N = (SELECT COUNT(DISTINCT (e2.empsalary))
FROM emptable e2 WHERE e2.empsalary >= e1.empsalary))

Note: you need to replace the value of N while running the query.
Example:
If you want highest paid employee details then value of N=1 then query will look like :

SELECT * FROM emptable e1 WHERE (1 = (SELECT COUNT(DISTINCT (e2.empsalary))
FROM emptable e2 WHERE e2.empsalary >= e1.empsalary))

No comments:

Post a Comment