Ads

01 January 2014

Find Nth Highest sal of emp

The following solution is for getting 5th highest sal from emp table ,

SELECT TOP 1 sal FROM (
SELECT DISTINCT TOP 5 sal
FROM emp ORDER BY sal DESC) a
ORDER BY sal


General Syntax to Find Nth Sal, N is the variable

SELECT TOP 1 sal FROM (
SELECT DISTINCT TOP n sal FROM emp
ORDER BY sal DESC) a
ORDER BY sal
where n > 1


Note :-n is always greater than one


No comments:

Post a Comment