Oracle FAQ Your Portal to the Oracle Knowledge Grid
HOME | ASK QUESTION | ADD INFO | SEARCH | E-MAIL US
 

Home -> Community -> Mailing Lists -> Oracle-L -> RE: problem using ROWNUM and ORDER BY clause together

RE: problem using ROWNUM and ORDER BY clause together

From: Mercadante, Thomas F <NDATFM_at_labor.state.ny.us>
Date: Fri, 31 May 2002 05:23:21 -0800
Message-ID: <F001.00470B13.20020531052321@fatcity.com>


Harvinder,

My experience has been that Oracle applies rownum first, then order by. So, first it retrieves records that pass the 'where' clause, then sorts them.

The results of your examples may be explained by the use of an index - try running explain plan against them to see if an index was used (because of the order-by clause).

The most important thing you need to realize is that you cannot depend on the order of data being retrieved in the same order it was inserted.

Raj's example is the correct way to solve your query request.

Tom Mercadante
Oracle Certified Professional

-----Original Message-----

Sent: Thursday, May 30, 2002 4:52 PM
To: Multiple recipients of list ORACLE-L

Scenario is we have a table having 100 rows and column id_po as unique column with distinct values betweem 1 and 100....... we we try
select id_po from table1 where rownum < 5 order by id_po; it gives result as:
1
2
3
4
and if we try
select id_acc,id_po from table1 where rownum < 5 order by id_po desc; 100
99
98
97

That implies oracle is first getting the result set and then apply order by and then rownum..............

But when we try
select id_acc,id_po from table1 where rownum < 2 order by id_po desc; result is:
1

where it should be 100 if above statement is true........

Thanks
--Harvinder

-----Original Message-----

Sent: Thursday, May 30, 2002 4:30 PM
To: Multiple recipients of list ORACLE-L

When you use rownum < 2 you are effectively selecting only one row and stopping after that. Also This is not the right way to do it, the right way is to use inline view with rownum condition and order by in outer query.

In your scenario, oracle will retrieve 2 rows and then sort them. These two rows can be anything and are not affected by the order by clause YET.

Ordering one row reminds of an assignment we had in our Graphics class, we were asked to implement object rotation, a colleague complained his program is right, but the object wasn't getting rotated. When we had a look on his screen, he was trying to rotate a circle !

Raj



Rajendra Jamadagni MIS, ESPN Inc.
Rajendra dot Jamadagni at ESPN dot com
Any opinion expressed here is personal and doesn't reflect that of ESPN Inc.

QOTD: Any clod can have facts, but having an opinion is an art!

-----Original Message-----

Sent: Thursday, May 30, 2002 4:08 PM
To: Multiple recipients of list ORACLE-L

problem is it works if we have rownum < 3(or any value >2) and only fails if we use rownum < 2...
--

Please see the official ORACLE-L FAQ: http://www.orafaq.com
--

Author: Harvinder Singh
  INET: Harvinder.Singh_at_MetraTech.com

Fat City Network Services    -- (858) 538-5051  FAX: (858) 538-5051
San Diego, California        -- Public Internet access / Mailing Lists

--------------------------------------------------------------------
To REMOVE yourself from this mailing list, send an E-Mail message
to: ListGuru_at_fatcity.com (note EXACT spelling of 'ListGuru') and in the message BODY, include a line containing: UNSUB ORACLE-L (or the name of mailing list you want to be removed from). You may also send the HELP command for other information (like subscribing).
--

Please see the official ORACLE-L FAQ: http://www.orafaq.com
--

Author: Mercadante, Thomas F
  INET: NDATFM_at_labor.state.ny.us
Fat City Network Services    -- (858) 538-5051  FAX: (858) 538-5051
San Diego, California        -- Public Internet access / Mailing Lists

--------------------------------------------------------------------
To REMOVE yourself from this mailing list, send an E-Mail message
to: ListGuru_at_fatcity.com (note EXACT spelling of 'ListGuru') and in the message BODY, include a line containing: UNSUB ORACLE-L (or the name of mailing list you want to be removed from). You may also send the HELP command for other information (like subscribing). Received on Fri May 31 2002 - 08:23:21 CDT

Original text of this message

HOME | ASK QUESTION | ADD INFO | SEARCH | E-MAIL US