RSS Feed

How To Select Random Records in SQL Server

I Found a useful method to select one or more random records using SQL Statements

In order to select 5 random records from a table try  this :

SELECT TOP 5 * FROM TableName ORDER By NEWID()

This syntax is for MS SQL Server, so i found other different syntaxes for MySQL , Oracle , etc:

MySQL:

SELECT * FROM TableName ORDER By RAND()LIMIT 5

Oracle:

SELECT columnName FROM (  SELECT columnName FROM TableName ORDER BY dbms_random.value ) WHERE rownum = 5


One Response to “How To Select Random Records in SQL Server”

  1. Thank you! I always wanted to show in my site something like that :)

Leave a Reply

*