Does SELECT retrieve rows in the order they are inserted ( Back to Basics -1 ) ?

AID: 6372
  • Status: Published

1546 points

  • Byanoospillai
  • TypeTips/Tricks
  • Posted on2011-06-21 at 18:13:07
In SQL Server, when rows are selected from a table, does it retrieve data in the order in which it is inserted?  Many believe this is the case.

Let us try to examine for ourselves with an example.

To get started, use the following script, which I ran on my sever to create a table OrderTest in database TempDB and, additionally, to insert 4 rows into the table.
USE TempDB 
GO
CREATE TABLE OrderTest(N1 INT NOT NULL, C1 VARCHAR(100) NOT NULL)
GO 
INSERT INTO OrderTest(N1, C1) VALUES(1, 'First Insert') 
GO 
INSERT INTO OrderTest(N1, C1) VALUES(10, 'Second Insert') 
GO 
INSERT INTO OrderTest(N1, C1) VALUES(2, 'Third Insert') 
GO 
INSERT INTO OrderTest(N1, C1) VALUES(4, 'Fourth Insert') 
GO 
                                    
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:

Select allOpen in new window



We are ready for the test.  Let us select data from the table.
USE TempDB 
GO
SELECT * FROM OrderTest 
GO
                                    
1:
2:
3:
4:

Select allOpen in new window

Fig1.1.bmp
  • 64 KB
  • TestOutput1
TestOutput1

The results are indeed retrieved in the order in which it is inserted.  Convinced?

Now I am going to add a primary key to the table.
USE TempDB 
GO
ALTER TABLE OrderTest ADD PRIMARY KEY(N1)
GO 
                                    
1:
2:
3:
4:

Select allOpen in new window



With the primary key in place, let us once again select data from the table using the previous select statement.
USE TempDB 
GO
SELECT * FROM OrderTest 
GO
                                    
1:
2:
3:
4:

Select allOpen in new window

Fig1.2.bmp
  • 61 KB
  • TestOutput2
TestOutput2

The rows are NOT returned in the order in which it is inserted!!  Surprising??

What made the difference?  

In the first case, the SQL Server query optimizer decided to do a table scan while it opted for a Clustered Index Scan in the latter case.   See the following execution plans for an illustration.
 
Fig1.3.bmp
  • 223 KB
  • ExecutionPlan1
ExecutionPlan1
Fig1.4.bmp
  • 258 KB
  • ExecutionPlan2
ExecutionPlan2


The verdict.

SQL server does not guarantee any order of output (unless you explicitly append an ORDER BY clause to the query to order your output in a desired way added).  Further, it is the SQL Server query optimizer that decides how the data should be retrieved.  Hence never assume that data will be returned in the order in which it is inserted.
    Asked On
    2011-06-21 at 18:13:07ID6372
    Tags

    SQL server 2005

    ,

    SQL

    ,

    SELECT

    ,

    Order of output

    Topic

    SQL Server 2005

    Views
    717

    Comments

    Add your Comment

    Please Sign up or Log in to comment on this article.

    Join Experts Exchange Today

    Gain Access to all our Tech Resources

    Get personalized answers

    Ask unlimited questions

    Access Proven Solutions

    Search 3.2 million solutions

    Read In-Depth How-To Guides

    1000+ articles, demos, & tips

    Watch Step by Step Tutorials

    Learn direct from top tech pros

    And Much More!

    Your complete tech resource

    See Plans and Pricing

    30-day free trial. Register in 60 seconds.

    Loading Advertisement...

    Top MS SQL Server 2005 Experts

    1. ScottPletcher

      195,617

      Guru

      8,500 points yesterday

      Profile
      Rank: Genius
    2. jogos

      176,191

      Guru

      668 points yesterday

      Profile
      Rank: Sage
    3. acperkins

      140,953

      Master

      1,000 points yesterday

      Profile
      Rank: Genius
    4. TempDBA

      113,707

      Master

      1,168 points yesterday

      Profile
      Rank: Sage
    5. matthewspatrick

      93,824

      Master

      1,600 points yesterday

      Profile
      Rank: Savant
    6. lcohan

      93,302

      Master

      2,000 points yesterday

      Profile
      Rank: Genius
    7. dtodd

      84,612

      Master

      0 points yesterday

      Profile
      Rank: Genius
    8. mwvisa1

      76,166

      Master

      0 points yesterday

      Profile
      Rank: Genius
    9. ValentinoV

      76,011

      Master

      1,800 points yesterday

      Profile
      Rank: Genius
    10. ralmada

      55,844

      Master

      400 points yesterday

      Profile
      Rank: Genius
    11. anujnb

      54,164

      Master

      0 points yesterday

      Profile
      Rank: Wizard
    12. angelIII

      53,846

      Master

      10 points yesterday

      Profile
      Rank: Elite
    13. EugeneZ

      53,602

      Master

      0 points yesterday

      Profile
      Rank: Genius
    14. HainKurt

      49,150

      0 points yesterday

      Profile
      Rank: Genius
    15. Buttercup1

      48,568

      0 points yesterday

      Profile
      Rank: Master
    16. huslayer

      40,600

      0 points yesterday

      Profile
      Rank: Sage
    17. appari

      39,400

      0 points yesterday

      Profile
      Rank: Genius
    18. tim_cs

      34,200

      0 points yesterday

      Profile
      Rank: Wizard
    19. wdosanjos

      33,836

      0 points yesterday

      Profile
      Rank: Genius
    20. dqmq

      31,136

      0 points yesterday

      Profile
      Rank: Genius
    21. Cluskitt

      30,940

      0 points yesterday

      Profile
      Rank: Wizard
    22. SJCFL-Admin

      30,877

      0 points yesterday

      Profile
      Rank: Master
    23. jimhorn

      29,975

      0 points yesterday

      Profile
      Rank: Genius
    24. Brichsoft

      28,107

      0 points yesterday

      Profile
      Rank: Sage
    25. momi_sabag

      27,903

      0 points yesterday

      Profile
      Rank: Genius

    Hall Of Fame