You are here:
SQL Server 2012 - Microsoft's latest database engine SQL Server 2012 - Microsoft's latest database engine

Microsoft SQL Server TSQL Random Rows and Records using NEWID()

If you are ever in a position where you would like to return random rows or records from a Microsoft SQL Server table, then you will probably want to use NEWID()

This function NewID(), returns a GUID (Globally Unique Identifier) thus allowing random rows and records to be returned. Lets look at a simple select query:

 

SET ROWCOUNT 10
SELECT col1, col2, col3
FROM tblMyTable
ORDER BY NEWID()
SET ROWCOUNT 0

Nice and easy! This code will return 10 random records or rows from tblMyTable.