Random Records from a Table

If you have ever had the need to return random records from a table such a sample data for test cases then the following T-SQL statement may come in handy.

SELECT TOP 10 * FROM northwind.dbo.orders ORDER BY NEWID()

This query will return ten random records from the Northwind Orders table.

As an aside a sort column in an ORDER BY can be specified as a name or column alias, an expression, or a nonnegative integer representing the position of the name, alias, or expression in the select list.