SQL Server tip: retrieving random rows from a table
Posted: (EET/GMT+2)
Usually when working with database, you want to explicitly tell the database which rows you want to retrieve. However, there are times when a random selection instead would be exactly what's needed.
Here's a little tip if you are using SQL Server: you can get any (usually, just a few) rows in random order if you ORDER BY using the Transact-SQL function NEWID().
For instance, with the Northwind sample database:
SELECT TOP 5 * FROM Customers ORDER BY NEWID()
The above query would return five random rows, and if you run the query again, another set of random rows. For instance, you might get:
GREAL Great Lakes Food Market FRANR France restauration PICCO Piccolo und mehr HILAA HILARION-Abastos TORTU Tortuga Restaurante
Hope this helps!