Friday, October 31, 2008

SQL Random Rows

Select a random row with MySQL:

SELECT column FROM table ORDER BY RAND() LIMIT 1

Select a random row with PostgreSQL:

SELECT column FROM table ORDER BY RANDOM() LIMIT 1

Select a random row with Microsoft SQL Server:

SELECT TOP 1 column FROM table ORDER BY NEWID()

Thanks to: http://www.petefreitag.com/item/466.cfm

No comments: