Finding Nth Highest Salary Query
SELECT max(sal) FROM tbltemp WHERE sal NOT IN
(SELECT distinct top 2 sal FROM tbltemp ORDER BY sal desc)
for eg. for 3rd highest top will be 3-1 =2 (top 2) therefore top N-1
Or
SELECT TOP 1 sal
SELECT DISTINCT TOP 2 sal
FROM tbltemp
ORDER BY sal DESC
) A
ORDER BY sal
(SELECT distinct top 2 sal FROM tbltemp ORDER BY sal desc)
for eg. for 3rd highest top will be 3-1 =2 (top 2) therefore top N-1
Or
SELECT TOP 1 sal
FROM (
SELECT DISTINCT TOP 2 sal
FROM tbltemp
ORDER BY sal DESC
) A
ORDER BY sal
Comments
Post a Comment