This question already has an answer here:
I want to select the number of rows which are greater than 3 by rownum function i_e "(rownum>3)" for example if there are 25 rows and I want to retrieve the last 22 rows by rownum function. but when I write the
select * from test_table where rownum>3;
it retrieve no row. can any one help me to solve this problem. thanks in advance
It is not working because: for the first row assumes the
ROWNUM
of 1 and since yourWHERE
clause isROWNUM>3
then this reduces to1>3
and the row is discarded. The subsequent row will then be tested against aROWNUM
of 1 (since the previous row is no longer in the output and now does not require a row number), which will again fail the test and be discarded. Repeat, ad nauseum and all rows fail theWHERE
clause filter and are discarded.If you want to assign the rows a
ROWNUM
then you need to do this is a sub-query:Or, if you want to order the results before numbering:
see this article for more samples On Top-n and Pagination Queries By Tom Kyte
In RDBMS there is no first or last rows. What you calls "raws" , actually is set(sets), they can be ordered or not.
rownum
is a function, which is just enumerates result set, it makes sense only after set is calculated, to order your set of data (rows) you should do it in your query beforerownum
call, you must tell DB what means for the order in particular select statement.