2
Vote

SELECT Sales.Customer

description

Hi!

I'm no expert in sql but can someone explain to me why do I get different CustomerID list with these two queries:

1

SELECT TOP 1000 [CustomerID]
      ,[PersonID]
      ,[StoreID]
      ,[TerritoryID]
      ,[AccountNumber]
      ,[rowguid]
      ,[ModifiedDate]
  FROM [AdventureWorks2012].[Sales].[Customer]

2

SELECT TOP 1000 [CustomerID]
   FROM [AdventureWorks2012].[Sales].[Customer]
Thanks for helping!

file attachments

comments

kevriley wrote May 16 at 4:21 PM

You are specifying top 1000, but with no ORDER BY clause, so essentially you are getting 1000 random rows.

The difference in execution plans means that you get a different 1000 rows.
The first query is a clustered index scan, the second a non-clustered index scan.

vikash_nda wrote Jul 10 at 5:34 PM

logically the results are same the only difference is the order they are shown to you. when you order by the second query you will see the same result. SInce the order by clause executes after select there is no chances of data being rearranged in both the cases.