| Return To Topics |
Originally Posted: 6/8/2006 7:50:35 AM |
| |
Last Updated: 6/8/2006 7:50:35 AM |
Subject:
Using TOP in a select statement
You Asked....
I understand that the TOP statement return x number of rows or x% of rows but how does SQL Server determine which rows are returned?
and we responded....
TOP is generally used in conjunction with an ORDER BY clause.
So if your SQL was:
SELECT TOP 10 * FROM Employees ORDER BY FirstName
then your result would be the first 10 "FirstName's" that are in the table.
Like I said TOP is generally used with an ORDER BY but if it is not then it is my understanding that a random resultset will be returned.
Sometimes that resultset may be the same but it is not guaranteed to always be the case.
Review By: Gary
>>>>>Begin Original Message<<<<<
I understand that the TOP statement return x number of rows or x% of rows but how does SQL Server determine which rows are returned?
>>>>>End Original Message<<<<<
Review On: 6/9/2006 1:49:09 AM
Review By: Gary
>>>>>Begin Original Message<<<<<
I understand that the TOP statement return x number of rows or x% of rows but how does SQL Server determine which rows are returned?
>>>>>End Original Message<<<<<
The TOP n option in a Select statement will return n number of rows from the result set. So if you use Where or Join options it will return the first n rows from the results of that query.
If you use an Order By clause I have found it to always return the same rows it each time otherwise it is not guaranteed to return the same results.
Review On: 6/9/2006 1:54:56 AM
|