IBM Mainframe Forum Index
 
Log In
 
IBM Mainframe Forum Index Mainframe: Search IBM Mainframe Forum: FAQ Register
 

Order of records if no option provided


IBM Mainframe Forums -> DB2
Post new topic   Reply to topic
View previous topic :: View next topic  
Author Message
Bharti P Manglani

New User


Joined: 13 Mar 2007
Posts: 16
Location: Pune

PostPosted: Thu Aug 09, 2007 2:23 pm
Reply with quote

Hi All,

I have a question:

Given the following table definition:
SALES
INVOICE_NO CHAR(20) NOT NULL
SALES_DATE DATE
SALES_PERSON CHAR(20)
REGION CHAR(20)
SALES INTEGER

If the following SELECT statement is executed, which of the following describes the order of the rows in the result data set produced?

SELECT * FROM sales:
A. The rows are sorted by INVOICE_NO in ascending order.
B. The rows are sorted by INVOICE_NO in descending order.
C. The rows are ordered based on when they were inserted into the table.
D. The rows are not sorted in any particular order.

I think the option should be C. But the answer provided in the book is D.

Please help. icon_sad.gif
Back to top
View user's profile Send private message
dbzTHEdinosauer

Global Moderator


Joined: 20 Oct 2006
Posts: 6966
Location: porcelain throne

PostPosted: Thu Aug 09, 2007 2:38 pm
Reply with quote

Craig Mullins DB2 Dev Guide wrote:

You cannot guarantee the order of the rows returned from a SELECT statement without an ORDER BY clause. At times SQL developers get confused when DB2 uses an index to satisfy a query and the results are returned in the desired order even without the ORDER BY clause. But, due to the nature of the DB2 optimizer, the access path by which the data is retrieved might change from execution to execution of an application program. If the access path changes, and ORDER BY is not specified, the results can be returned in a different (non-desired) order. For this reason, always code the ORDER BY clause when the sequence of rows being returned is important.


If you are basing your thinking on knowledge that VSAM inserts do not reorder the base but only the index, you are forgetting about REORG.
Back to top
View user's profile Send private message
dbzTHEdinosauer

Global Moderator


Joined: 20 Oct 2006
Posts: 6966
Location: porcelain throne

PostPosted: Thu Aug 09, 2007 2:55 pm
Reply with quote

by the way, ORDER BY in not an option. The proper term is clause
Back to top
View user's profile Send private message
Bharti P Manglani

New User


Joined: 13 Mar 2007
Posts: 16
Location: Pune

PostPosted: Thu Aug 09, 2007 3:11 pm
Reply with quote

Hi,

Thanks a lot!! And yes, order by is a clause not an option. Thanks again for that!! icon_biggrin.gif
Back to top
View user's profile Send private message
View previous topic :: :: View next topic  
Post new topic   Reply to topic View Bookmarks
All times are GMT + 6 Hours
Forum Index -> DB2

 


Similar Topics
Topic Forum Replies
No new posts Compare only first records of the fil... SYNCSORT 7
No new posts Pulling a fixed number of records fro... DB2 2
No new posts SCOPE PENDING option -check data DB2 2
No new posts Rotate partition-logical & physic... DB2 0
No new posts OUTFIL with SAVE option DFSORT/ICETOOL 7
Search our Forums:

Back to Top