learn how to get random records in sql server

In this ms sql server tutorial you will learn how to get random records in sql server. Sometimes you may need to get random records from table in ms sql server database. To do this in ms sql server is quite easy. Let's have a look over example below in which i am getting five random records from visitors table located in ms sql server database.

Get random records in ms sql server

SELECT TOP 5 firstname, lastname, country FROM visitors
ORDER BY NEWID()



The NEWID() system function makes a unique value of the type uniqueidentifier. There is no need to add a new column in your table to get the random records. you just have to include the NEWID() system function in the ORDER BY clause of your SELECT statement. I hope you will like this tutorial.

0 comments: