Thursday, August 10, 2017

Query to find nth highest salary in sql | 3rd highest salary ?

Here I am providing generic query to fined any salary you want to get


select * from ( select distinct (sal),ROW_NUMBER() OVER (order by salary desc) rnum from employee ) where rnum=2


You can change the rnum value whichever you want.


In the same way , if you want find nth lowest salary from the table simply put asc in the place of desc
select * from ( select distinct (sal),ROW_NUMBER() OVER (order by salary asc) rnum from employee ) where rnum=2

No comments: