Some of the examples were taken from this SOen response from @ YaakovEllis
I've already warned you that this has not been done performance testing, which can probably be a bit tricky if you need to check out many things. I want to run my own tests and maybe suggest different ways.
Another detail, the use of LIMIT
, TOP 1
, rownum
, etc is only to indicate that there was limitation of results, after all the intention is to get random results and not only to order in a random fashion
MySQL
Select random MySQL:
SELECT column FROM table
ORDER BY RAND()
LIMIT 1
PostgreSQL
Select random in PostgreSQL:
SELECT column FROM table
ORDER BY RANDOM()
LIMIT 1
SQL Server
Select random in Microsoft SQL Server:
SELECT TOP 1 column FROM table
ORDER BY NEWID()
IBM DB2
Select random in IBM DB2
SELECT column, RAND() as IDX
FROM table
ORDER BY IDX FETCH FIRST 1 ROWS ONLY
Oracle
Select Random in Oracle:
SELECT column FROM
( SELECT column FROM table
ORDER BY dbms_random.value )
WHERE rownum = 1
SQLite
Select random in Sqlite:
SELECT column FROM table
ORDER BY RANDOM() LIMIT 1