Thursday, January 27, 2011

SQL to Select a random row from a database table

 

There are lots of ways to select a random record or row from a database table. Here are some example SQL statements.

Select a random row with MySQL:

Syntax:

SELECT * FROM TABLE_NAME ORDER BY RAND() LIMIT 10;

Select a random row with MySQL

Select a random row with Microsoft SQL Server:

Syntax:

SELECT TOP 1 column FROM table ORDER BY NEWID()

random_row_ Microsoft_SQL_Server

Select a random record with Oracle:

SELECT column FROM ( SELECT column FROM table ORDER BY dbms_random.value ) WHERE rownum = 1;

random_row_Oracle

No comments:

Post a Comment