Monday, 26 August 2013

How do I find the nth row from a sql query?

How do I find the nth row from a sql query?

I have a table in which data is something like this:
Name Salary
Tom 10000
John 20000
Ram 20000
Danny 15000
Sandy 14000
Riddle 15000
I can find 2nd highest salary using cte;
with cte
as
(
select ROW_NUMBER() over (order by Salary desc) as r,
* from Employee e
)
select * from cte where r=2
But this gives the result 'Ram' with 20000 salary. How can I find a
different record; i.e. for Danny and Riddle with the 15000 salary?

No comments:

Post a Comment