Two similar queries returning completely different results!!!

  • Hi

    I was wondering if someone could help me with this.

    I have a table called history and it contains about 70 columns, when i do a select * from history 198 rows are returned and the top row has the values below

    co1 | col2 | col3 | col4 | col5 | col6 | col7 | col8 | col9 | col10 | col11 | col12

    454 | 550 | 688 | 310 | 526 | 694 | 858 | 898 | 766 | 476 | 362 | 198

    Now this is the weird part, when i use a select col1, colu2 etc from history the top values are completely different as shown below.

    col1 | col2 | col3 | col4 | col5 | col6 | col7 | col8 | col9 | col10 |col11 | col12

    231 | 370 | 529 | 899 | 890 | 961 | 759 | 572 | 338 | 751 | 599 | 506

    So when you compare the two queries the results are completely different surely this should not be the case.

    Can anyone shed any light on the reason for the this

    Thanks in advance

    __________________________________________________________________________________
    Steve J
    Jnr Developer
    BSc(Hon)

  • Ordering is not guaranteed when you do not specify order by clause. So if you have no order by clause, then the query optimizer is free to return rows in any order.


    Forever trying to learn
    My blog - http://www.cadavre.co.uk/
    For better, quicker answers on T-SQL questions, click on the following...http://www.sqlservercentral.com/articles/Best+Practices/61537/
    For better, quicker answers on SQL Server performance related questions, click on the following...http://www.sqlservercentral.com/articles/SQLServerCentral/66909/

  • Do you have an ORDER BY?

    If not, SQL makes absolutely no guarantees about the ordering that the data will be returned in. It'll be whatever order the rows are based on the query operators chosen.

    If you need a specific order, specify ORDER BY. Otherwise the order can and will change.

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • Thansk for your reply GilaMonster , skcadavre

    To be honest i dont know where i didnt think of that lol, i'll try that now

    __________________________________________________________________________________
    Steve J
    Jnr Developer
    BSc(Hon)

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply