© copyright 03.Jan.2009 by Paul Bradley filed under Microsoft SQL Server
There are lots of valid reasons why you would want to select random records from your Microsoft SQL Server database, for example you might want to randomly select three staff from your staff database as winners in the company staff lottery, or you may want to pull random adverts from your inventory of web site advertisers to display on your web site.
The example SQL code below is similar to what I use on my hotel web site to pull five random hotels from the Property table to display as featured hotels on the home page.
SELECT TOP 5 HotelName, Description, URL
FROM Property ORDER BY NEWID()
About the Author
Paul Bradley is a VB.NET software developer living and working in Cumbria. He has over 20 years programming experience.
He also produces e-learning videos at Code by Example
and Linux by Example