How to get records randomly from the oracle database?

Bhadra picture Bhadra · Mar 26, 2012 · Viewed 138.6k times · Source

I need to select rows randomly from an Oracle DB.

Ex: Assume a table with 100 rows, how I can randomly return 20 of those records from the entire 100 rows.

Answer

cagcowboy picture cagcowboy · Mar 26, 2012
SELECT *
FROM   (
    SELECT *
    FROM   table
    ORDER BY DBMS_RANDOM.RANDOM)
WHERE  rownum < 21;