Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 SELECT TOP 5 not working right

Author  Topic 

Rock_query
Yak Posting Veteran

55 Posts

Posted - 2014-12-27 : 20:21:28
I am using the AdventureWorks2012 database. I am testing the TOP clause. My understanding is that this should return the first nth number of rows, but the results are not consistent with this.

I am testing this on the table Production.Product. The first 5 values in the ProductID column are: 1,2,3,4 and 316.

However I do not get these results with the following code:


SELECT TOP (5) ProductID
FROM Production.Product


I am getting: 980, 365, 771, 404 and 977.

How is SSMS getting these results? Regardless of any sorting, I thought that the TOP clause is always supposed to return the top nth number of rows that are showing?

bitsmed
Aged Yak Warrior

545 Posts

Posted - 2014-12-28 : 07:17:16
If you don't specify a sort sequence (order by), the database engine is allowed to return the data in which ever way it sees fit
Go to Top of Page

gbritton
Master Smack Fu Yak Hacker

2780 Posts

Posted - 2014-12-28 : 10:15:30
"The first 5 values in the ProductID column are: 1,2,3,4 and 316."

Why do you say that? By definition, a table is a set, which by definition has no ordering and so has no "first 5 values"
Go to Top of Page
   

- Advertisement -