How to Select a Random Row
I’ve seen similar questions in programming forums about how to select a random row from a table. Most RDBMS have functions to generate random numbers, so with a simple query we can select a random row(s):
1 |
SELECT * FROM hr.employees ORDER BY DBMS_RANDOM.VALUE; |
The above command will sort the HR.EMPLOYEES table in random order, so we’ll be able to select a random. If you want to limit the number of returning rows, you can use subquery method:
1 2 3 |
SELECT * FROM (SELECT * FROM hr.employees ORDER BY DBMS_RANDOM.VALUE) WHERE ROWNUM <= :X; |