Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Oracle random row from table

Tags:

random

oracle

row

I found this solution for selecting a random row from a table in Oracle. Actually sorting rows in a random manner, but you can fetch only the first row for a random result.

SELECT *
FROM table
ORDER BY dbms_random.value;

I just don't understand how it works. After ORDER BY it should be a column used for sorting. I see that "dbms_random.value" returns a value lower than zero. This behavior can be explained or is just like that?

Thanks

like image 367
Adrian Avatar asked Oct 13 '11 06:10

Adrian


1 Answers

you could also think of it like this:

SELECT col1, col2, dbms_random.value
FROM table
ORDER BY 3

In this example the number 3 = the third column

like image 184
Kevin Burton Avatar answered Sep 28 '22 08:09

Kevin Burton