ROWNUM applies to the output of the selection process, not the input. So for example if you select any 5 records from a table, they will
always have ROWNUM values from 1 to 5, in the order the records were found. If the query has an ORDER BY clause, this is applied
after the ROWNUMs have been assigned, hence:
SQL> select dname, rownum from dept;
DNAME ROWNUM
-------------- ----------
ACCOUNTING 1
RESEARCH 2
SALES 3
OPERATIONS 4
SQL> select dname, rownum from dept order by dname;
DNAME ROWNUM
-------------- ----------
ACCOUNTING 1
OPERATIONS 4
RESEARCH 2
SALES 3
SQL> select dname, rownum from dept where dname='SALES';
DNAME ROWNUM
-------------- ----------
SALES 1
SQL> select dname, rownum from dept where dname='ACCOUNTING';
DNAME ROWNUM
-------------- ----------
ACCOUNTING 1
See? If you think of the query processor as a program it looks like this:
Code:
-- Select records
ROWNUM = 0
loop
Get next row
If row matches WHERE clause then
ROWNUM = ROWNUM+1
output(ROWNUM) = this row
end if
end loop