|
|
|
|
|
Re: diff rowid & rownum [message #191298 is a reply to message #191210] |
Tue, 05 September 2006 10:23 |
gold_oracl
Messages: 129 Registered: July 2006 Location: Westborough, MA
|
Senior Member |
|
|
Maheer,
are you sure that ROWNUM is applied before order by clause.
But, i believe that it would be applied after order by cluase(please correct me if i'm wrong).
please check the following snipset.
SQL> SELECT ROWNUM,A,B FROM TAB1;
ROWNUM A B
---------- ---------- ----------
1 1 A1
2 1 B1
3 1 C1
4 2 A2
5 2 B2
SQL> SELECT ROWNUM,A,B FROM TAB1 ORDER BY 2 DESC;
ROWNUM A B
---------- ---------- ----------
1 2 B2
2 2 A2
3 1 C1
4 1 B1
5 1 A1
Thanks,
Thangam
|
|
|
|
|
|
Re: diff rowid & rownum [message #191372 is a reply to message #191298] |
Wed, 06 September 2006 01:17 |
|
Maaher
Messages: 7065 Registered: December 2001
|
Senior Member |
|
|
gold_oracl wrote on Tue, 05 September 2006 17:23 | Maheer,
are you sure that ROWNUM is applied before order by clause.
But, i believe that it would be applied after order by cluase(please correct me if i'm wrong).
|
SQL> SELECT ROWNUM
2 , region_id
3 , region_name
4 FROM regions
5 ORDER BY region_name
6 /
ROWNUM REGION_ID REGION_NAME
---------- ---------- -------------------------
2 2 Americas
3 3 Asia
1 1 Europe
4 4 Middle East and Africa
SQL> ed
Wrote file afiedt.buf
1 SELECT ROWNUM
2 , region_id
3 , region_name
4 FROM regions
5* ORDER BY 3
SQL> /
ROWNUM REGION_ID REGION_NAME
---------- ---------- -------------------------
2 2 Americas
3 3 Asia
1 1 Europe
4 4 Middle East and Africa
Seems like I was right
The reason why you witnessed the behaviour that a select with an ORDER BY descending still produces the row numbers in ascending order can be explained if you have an index on the A column. That would cause Oracle to retrieve the rows in the order desired by using that index. No explicit sort is necessary in that case. You can verify this by doing an explain plan for the select. The row numbers will still be retrieved before an explicit sort takes place. Oracle just takes a shortcut.
MHE
[Updated on: Wed, 06 September 2006 01:31] Report message to a moderator
|
|
|
|
Re: diff rowid & rownum [message #191540 is a reply to message #191415] |
Thu, 07 September 2006 01:27 |
|
Maaher
Messages: 7065 Registered: December 2001
|
Senior Member |
|
|
By the way, Littlefoot has a point: if you read the sticky (tips&tricks) you will understand what he means: search the Oracle documentation, search the forum, ... Most questions (certainly generic ones) are already asked and answered before.
link 1
link 2
link 3
link 4
MHE
|
|
|