Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Select statement - Will it be sorted by primary key by default?

Former Member
0 Likes
1,841

Hi,

Could any of you help me to know the following info?

I have a select statement as follows:

select * from ...into table int_tab where...

Once the select statement is executed, can I assume that the contents of int_tab is automatically sorted by the primary key of the table? Also please tell me how to prove it.

Please note that the select statement doesn't contain order by clause.

Thanks,

Thamarai

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
1,261

Since the SELECT statement supports the ORDER BY PRIMARY KEY clause, I don't think you can assume that it will be ordered by the primary key if you omit it.

Rob

Hi,

Could any of you help me to know the following info?

I have a select statement as follows:

select * from ...into table int_tab where...

Once the select statement is executed, can I assume that the contents of int_tab is automatically sorted by the primary key of the table? Also please tell me how to prove it.

Please note that the select statement doesn't contain order by clause.

Thanks,

Thamarai

3 REPLIES 3
Read only

Former Member
0 Likes
1,261

Yes you can assume that.

Records are always fetched in the order, sorted by primary key.

The reason for this is an Index is created by default for the primary key and hence they are sorted.

If you are adding records in internal table after fetching data, then you can use SORTED TABLE.

There's no method to prove it but you can try fetching data from different tables by using different WHERE conditions.

I mean to say that just play with the select statement and it will be always sorted by primary key unless you specify ORDER BY clause.

Read only

Former Member
0 Likes
1,261

By default it will take if you do not have any where condition and will not related to primary key..

if you keep where condition as per condition and then sorting will not work

i mean sorting will not take automatically and sorting is not related to primary key.

for better understanding please keep sort command after completion of sql query.

Reward Points if it is helpful

Thanks

Seshu

Read only

Former Member
0 Likes
1,262

Since the SELECT statement supports the ORDER BY PRIMARY KEY clause, I don't think you can assume that it will be ordered by the primary key if you omit it.

Rob