SQL Server > Query

Retrieve random records in SQL Server

How to retrieve random records from the database table in SQL Server?


To retrieve random records from database table, we use NEWID inbuilt function.

Execute below statement in Query window and we will get random two records from the PersonalDetails table. If we need more or less, simply change the value of “2” in the query.

SELECT TOP 2 * FROM PersonalDetails ORDER BY NEWID()

NEWID() function randomly creates a unique ID and sort the record based on that and list TOP 2 records from the database table.

 Views: 1405 | Post Order: 55



Write for us