Link to home
Start Free TrialLog in
Avatar of sakthikumar
sakthikumar

asked on

Effect of rownum operator in query.

Hi,

Look at the below two queries.

select * from (
select inv_seq,inv_nm_txt from invoice
where inv_nm_txt like 'A%'
order by inv_seq)
where rownum < = 100

select inv_seq,inv_nm_txt from invoice
where inv_nm_txt like 'A%'
and rownum <=100
order by inv_seq

Will both the queries produce the same results?
Would there be any difference as of rownum in both queries is concerned?
Avatar of TommySzalapski
TommySzalapski
Flag of United States of America image

They will be different. The rownum column is populated after the order by.
Typo, should read
They will be different. The rownum column is populated BEFORE the order by.
So in the second query the numbers in rownum will not be in order. They will represent the original order before the sort.
Of course, in the first query the numbers in rownum will be sorted along with the inv_seq.
Rownum is a pseudo column of the result set and not of the table. As such, the rownum does not have to be the same for a query that uses a subselect and one that doesn't. However, in your example, because the subquery is doing an order by, I believe they will be the same, although that may also not be reliable if, for example, the table is partitioned.
Even the exact same query with ROWNUM can give you different results. For instance, say you do select * from big_table where rownum<=100 order by <anything>. Then insert one row and re-run the select. If the inserted row happened to go to the first data blocks of the table segment, it will appear in the results. If it happened to get located after the "first 100 rows", the results won't change.

ROWNUM just reads the first N records from a result set. No repeatability is guaranteed.
Avatar of Mitali05
Mitali05

Both the queris will fecth different results.
Reason being
In the 1st query it will return the 1st 100 rows whose inv_nm_txt like 'A%'
In the 2nd Query it scan only the 1st 100 rows of table invoice to find the rows that match the condition inv_nm_txt like 'A%' and return the result in ascending order.
Examine output of query in this example, it will solve your doubts

SQL> create table temp(a number,b char(10));
Table created.

SQL> insert into temp values(1,'a');
1 row created.

SQL> insert into temp values(3,'c');
1 row created.

SQL> insert into temp values(4,'d');
1 row created.

SQL> commit;
Commit complete.

SQL> select rownum,temp.* from temp;

    ROWNUM          A B
---------- ---------- ----------
         1          1 a
         2          3 c
         3          4 d

SQL> select rownum,temp.* from temp order by b desc;

    ROWNUM          A B
---------- ---------- ----------
         3          4 d
         2          3 c
         1          1 a

SQL> select rownum,d.* from (select temp.* from temp order by b desc) d;

    ROWNUM          A B
---------- ---------- ----------
         1          4 d
         2          3 c
         3          1 a

Open in new window

Both queries return 100 rows.

But indeed the results are different:

The first query sorts all rows which match the WHERE-clause and then returns the first 100 of them

The second query takes the first 100 rows which match the WHERE-clause and then returns them according to the sort order
As I said above
Both the queris will fecth different results.
Reason being
In the 1st query it will return the 1st 100 rows whose inv_nm_txt like 'A%'
In the 2nd Query it scan only the 1st 100 rows of table invoice to find the rows that match the condition inv_nm_txt like 'A%' and return the result in ascending order
And if there are 100 matching records then definately both queries will return 100 rows.
ASKER CERTIFIED SOLUTION
Avatar of Devinder Singh Virdi
Devinder Singh Virdi
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial