Tech and travel

Selecting random rows

2007-08-01

If you want to select random rows from a table, you can order the rows using the dbms_random.value function :

SELECT column
  FROM table
 ORDER BY dbms_random.value

To get 20 random rows from the table, we can add a condition on rownum. This selects the first 20 rows :

SELECT column
  FROM ( SELECT column
           FROM table
          ORDER BY dbms_random.value )
 WHERE rownum < 21

With thanks to this site.

Copyright (c) 2024 Michel Hollands