I have tried the below query:
select empno from (
select empno
from emp
order by sal desc
Select * From (SELECT *,
ROW_NUMBER() OVER(ORDER BY column_name DESC) AS mRow
FROM table_name
WHERE condition) as TT
Where TT.mRow=2;
You can use RANK
or DENSE_RANK
to achieve what you are trying to achieve here.
To explain this behaviour, we need to understand how Oracle processes ROWNUM. When assigning ROWNUM to a row, Oracle starts at 1 and only increments the value when a row is selected; that is, when all conditions in the WHERE clause are met. Since our condition requires that ROWNUM is greater than 2, no rows are selected and ROWNUM is never incremented beyond 1.
The bottom line is that conditions such as the following will work as expected.
.. WHERE rownum = 1;
.. WHERE rownum <= 10;
While queries with these conditions will always return zero rows.
.. WHERE rownum = 2;
.. WHERE rownum > 10;
Quoted from Understanding Oracle rownum
You should modify you query in this way in order to work:
select empno
from
(
select empno, rownum as rn
from (
select empno
from emp
order by sal desc
)
)
where rn=2;
EDIT: I've corrected the query to get the rownum after the order by sal desc
select empno from(
select empno,rownum as rum
from emp,
order by sal desc
)
where rum=2;
Select Second Row From a Table in Oracle
SELECT *
FROM (SELECT * FROM emp ORDER BY rownum DESC)
WHERE rownum=1
try this way it's working 100% SQL> SELECT * FROM STUD;
RNUMBER SNAME MARKS
104 mahesh 85
101 DHANU 20
102 BHARATH 10
100 RAJ 50
103 GOPI 65
SQL> select * from(select MARKS,ROWNUM AS RS from ( select * from stud order by marks desc)) where RS=2;
MARKS RS
65 2
SQL>