Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in SQL by (20.3k points)

How to find third or nth maximum salary from salary table(EmpID,EmpName,EmpSalary) in Optimized way?

1 Answer

0 votes
by (40.7k points)

If you want a single row then use ROW_NUMBER or DENSE_RANK(for all related rows):

WITH CTE AS

(

    SELECT EmpID, EmpName, EmpSalary,

           RN = ROW_NUMBER() OVER (ORDER BY EmpSalary DESC)

    FROM dbo.Salary

)

SELECT EmpID, EmpName, EmpSalary

FROM CTE

WHERE RN = @NthRow

Browse Categories

...