select * from ( select mystring , rownum rn FROM teststring ) where rn = 2;
Your query returns rows randomly without a specified order. A heap-organized table is a table with rows stored in no particular order. This is a standard Oracle table.
From Oracle documentation,
Without an order_by_clause, no guarantee exists that the same query executed more than once will retrieve rows in the same order.
delete from testring where rownum=2;
Your above query shows you do not understand how ROWNUM works.
Your query will never delete any rows.
A ROWNUM value is assigned to a row after it passes the predicate phase of the query but before the query does any sorting or aggregation. Also, a ROWNUM value is incremented only after it is assigned, which is why the following query will never return a row:
select *
from t
where ROWNUM = 2;
Because ROWNUM = 2
is not true for the first row, ROWNUM
does not advance to 2. Hence, no ROWNUM
value ever gets to be greater than 1.
How to correctly use ROWNUM:
As you wanted to select a row based on ROWNUM, you could do something like pagination:
SQL> SELECT empno
2 FROM
3 (SELECT empno, sal, ROWNUM AS rnum FROM
4 ( SELECT empno, sal FROM emp ORDER BY sal
5 )
6 )
7 WHERE rnum =2;
EMPNO
----------
7900
It happens in three levels:
- Innermost sub-query first sorts the rows based on the ORDER BY clause.
- In second level, the sub-query assigns ROWNUM
- The outermost query filters the rows based on the ROWNUM given by inner query which is no more a pseudo-column but the sub-query resultset.