Monday, January 19, 2009

How to find the Nth Highest salary


MS-sql 
 SELECT TOP 1 salary 
    FROM (
    SELECT DISTINCT TOP n salary 
        FROM employee
        ORDER BY salary DESC) a 
ORDER BY salary

Mysql
In Mysql there is no function called TOP, LIMIT function is used instead of TOP function

Query syntax
SELECT DISTINCT column name from table name 
ORDER BY column name DESC 
LIMIT n-1,1

Query to find 4th highest salary
SELECT DISTINCT salary from emp
ORDER BY salary DESC
LIMIT 3,1

No comments:

Post a Comment