Randomize Records Easily

Ok, you need to select 5 different records at random from a table and no record should be repeated?

Tip from Bob Osola
------------------
You could use all kinds of different ways to go about this but most of the time the simplest solution is the best.

SELECT TOP 5 * FROM yourtable ORDER BY NEWID()


Additional by Bob Barrows
-------------------------
NEWID (T-SQL)
Creates a unique value of type uniqueidentifier.

Using "Order By NewID()" causes a uniqueidentifier to be assigned to each row returned by the query. The query initially returns all the rows in the table. Sorting by the uniqueidentifier essentially randomizes the records. Then Top 5 causes only the first 5 records to be returned.

To see it all happening
-----------------------
SELECT TOP 5 NEWID() AS Rand, *
FROM TableName
ORDER BY Rand

Source: Bob Osola, Additional by Bob Barrows
Viewed 5472 times