How to select the first 10 rows of a table without sorting?

Possible duplicate:
How to select the last 5 rows in a table without sorting?

I want to select the top 10 records from a table in SQL Server without placing the table in ascending or descending order.

0


source to share


3 answers


if random order is required you can try



select top 10 * from [tablename] order by newid()

      

+11


source


The question doesn't make sense. In SQL, a table has no implicit ordering. They can also be returned randomly, semantically.

Limiting results to the first 10 rows returned depends on the SQL server dialect. On MS-SQL server the keyword TOP is used, whereas in MySQL you use LIMIT, in Oracle you have to use ROWNUM etc.



Please provide more details on what exactly you are trying to accomplish.

+8


source


SELECT TOP 10 <requiredfieldListHere> FROM <TheTableNameHere>

If you have a clustered index, this will return the first 10 records in the table. Note, however, that this will be in bad shape. A relational table should not be considered to have any particular ordering. If you don't have a clustered index, it might return the first 10 records, but it might just return a random set.

If you are not satisfied that you will return all 10 records from the table, then you should ORDER BY your query. This is true even if you have a clustered index, as it may be dropped or changed in the future.

+4


source







All Articles