Oracle increments rownum
each time it adds a row to the result set. So saying rownum < 5
is fine; as it adds each of the first 5 rows it increments rownum
, but then once ruwnum = 5
the WHERE
clause stops matching, no more rows are added to the result, and though you don't notice this rownum
stops incrementing.
But if you say WHERE rownum > 5
then right off the bat, the WHERE
clause doesn't match; and since, say, the first row isn't added to the result set, rownum
isn't incremented... so rownum
can never reach a value greater than 5 and the WHERE
clause can never match.
To get the result you want, you can use row_number() over()
in a subquery, like
select *
from (select row_number() over() rn, -- other values
from table
where -- ...)
where rn > 5
Update - As noted by others, this kind of query only makes sense if you can
control the order of the row numbering, so you should really use row_number() over(order by
something)
where something is a useful ordering key in deciding which records are "the first 5 records".