Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 550679
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T11:18:51+00:00 2026-05-13T11:18:51+00:00

I was wondering how sql server sorts it’s data. I noticed that if I

  • 0

I was wondering how sql server sorts it’s data.
I noticed that if I have a table that doesn’t contain the column “Id” and you select data without “ORDER BY” sql server doesn’t automatically sort on the primary column.

Does anyone know what rule sql server follows to sort it’s data?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T11:18:51+00:00Added an answer on May 13, 2026 at 11:18 am

    Although it’s good to wonder about how it might be explained that you often see the same order, I’d like to point out that it never a good idea to rely on implicit order caused by the particular implementation of the underlying database engine. In other words, its nice to know why, but you should never ever rely on it. For MS SQL, the only thing that reliably delivers the rows in a certain order, is an explicit ORDER BY clause.

    Not only do different RDMBS-es behave differently, one particular instance may behave differently due to an update (patch). Not only that, even the state of the RDBMS software may have an impact: a “warm” database behaves differently than a “cold” one, a small table behaves differently than a large one.

    Even if you have background information about the implementation (ex: “there is a clustered index, thus it is likely the data will be returned by order of the clustered index”), there is always a possibility that there is another mechanism you don’t know about that causes the rows to be returned in a different order (ex1: “if another session just did a full table scan with an explicit ORDER BY the resultset may have been cached; a subsequent full scan will attempt to return the rows from the cache”; ex2: “a GROUP BY may be implemented by sorting the data, thus impacting the order the rows are returned”; ex3: “If the selected columns are all in a secondary index that is already cached in memory, the engine may scan the secondary index instead of the table, most likely returning the rows by order of the secondary index”).

    Here’s a very simple test that illustrates some of my points.

    First, startup SQL server (I’m using 2008). Create this table:

    create table test_order (
        id int not null identity(1,1) primary key
    ,   name varchar(10) not null 
    )
    

    Examine the table and witness that a clusted index was created to support the primary key on the id column. For example, in sql server management studio, you can use the tree view and navigate to the indexes folder beneath your table. There you should see one index, with a name like: PK__test_ord__3213E83F03317E3D (Clustered)

    Insert the first row with this statement:

    insert into test_order(name)
    select RAND()
    

    Insert more rows by repeating this statement 16 times:

    insert into test_order(name)
    select RAND()
    from   test_order
    

    You should now have 65536 rows:

    select COUNT(*) 
    from   test_order
    

    Now, select all rows without using an order by:

    select *
    from   test_order
    

    Most likely, the results will be returned by order of the primary key (although there is no guarantee). Here’s the result I got (which is indeed by order of primary key):

    #      id    name
    1      1     0.605831
    2      2     0.517251
    3      3     0.52326
    .      .     .......
    65536  65536 0.902214
    

    (the # is not a column but the ordinal position of the row in the result)

    Now, create a secondary index on the name column:

    create index idx_name on test_order(name)
    

    Select all rows, but retrieve only the name column:

    select name
    from   test_order
    

    Most likely the results will be returned by order of the secondary index idx_name, since the query can be resolved by only scanning the index (i.o.w. idx_name is a covering index). Here’s the result I got, which is indeed by order of name.

    #      name
    1      0.0185732
    2      0.0185732
    .      .........
    65536  0.981894
    

    Now, select all columns and all rows again:

    select * 
    from test_order
    

    Here’s the result I got:

    #      id    name
    1      17    0.0185732
    2      18    0.0185732
    3      19    0.0185732
    ...    ..    .........
    

    as you can see, quite different from the first time we ran this query. (It looks like the rows are ordered by the secondary index, but I don’t have an explanation why that should be so).

    Anyway, the bottom line is – don’t rely on implicit order. You can think of explanations why a particular order can be observed, but even then you can’t always predict it (like in the latter case) without having intimate knowledge of implementation and runtime state.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 387k
  • Answers 388k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Use dns_get_record array dns_get_record ( string $hostname [, int $type… May 15, 2026 at 12:16 am
  • Editorial Team
    Editorial Team added an answer There is no way to prevent uploading of malicious files.… May 15, 2026 at 12:16 am
  • Editorial Team
    Editorial Team added an answer You are trying to access the variable $db which is… May 15, 2026 at 12:16 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.