Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Oracle rand() function

I tried to select a random data from table employees with query rand() but it can happen

SELECT email FROM employees
ORDER BY RAND()
LIMIT 1;

and the output is:

ORA-00933: SQL command not properly ended
00933. 00000 - "SQL command not properly ended"

Can somebody tell me why??

like image 931
alvin Christianto Avatar asked Oct 22 '25 23:10

alvin Christianto


1 Answers

The Oracle equivalent of rand() is dbms_random.value.

The Oracle equivalent of limit is either a subquery with rownum or (in Oracle 12) fetch first xx row only. So, one of these should work:

select email
from employees
order by dbms_random.value
fetch first 1 row only;

or:

select email
from (select email
      from employees
      order by dbms_random.value
     ) e
where rownum = 1
like image 92
Gordon Linoff Avatar answered Oct 24 '25 15:10

Gordon Linoff



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!