You are here -> Home > Developer Center > Randomly Select Records From a Table

Randomly Select Records From a Table
By: Jason Nowicki This article has been viewed 665 times.

If you want SQL Server to randomly select some records from a table so you don't have to do it in code then use this script:

Select TOP 1 <Field> From <Table> WHERE <If you want a where clause> ORDER BY NEWID()

The Order by NewID() part is where the random selection happens. This script will return only 1 record but you can replace the TOP 1 with TOP N to return as many randomly selected records from your database.

 

How helpful was this article?