For Finding nth highest salary I am using
select salary from
(
select distinct ROW_NUMBER() over (order by salary desc) as rownum,Salary
from Employee
)a
where rownum=2
However if i have same salary like
70000
70000
60000
50000
50000
When executing the query i am getting second highest salary
70000 instead 60000
how to avoid duplicates?
Although Mark has already provided one answer, I’d say that you’re using the wrong function. You don’t want the row number, you want the RANK. And based on how you want to handle duplicates, specifically you should be using DENSE_RANK.
E.g.: