0 votes
1 view
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.3k 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

Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...