How to select random rows from an SQL Table?

December 22, 2008 09:52 by akashheranjal
IN SQL Server, it is easy to select one random record or a sequence of random records from a database table. For example if you have requirement to pick a single quiz winner randomly from the database table or you are making an online test application and need to pick X number of questions randomly from the database table; you can certainly use the below SQL queries…


To select a single random record from a table run the below

Select TOP 1 [IdentityColumnName] from [TableName] Order By NEWID()



To select a sequence random record from a table run the below (where X is the number of records you want to fetch)

Select TOP X [IdentityColumnName] from [TableName] Order By NEWID()

Currently rated 4.0 by 2 people

  • Currently 4/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Comments

Add comment


 

  Country flag

biuquote
  • Comment
  • Preview
Loading