Pages

Thursday, August 18, 2016

Randomly select rows from a table


Summary 
If you need to randomly select one or more rows from one of your tables, then you can use the following query. As an example we want to show an employee, randomly selected from the EMP table:
SELECT *
 FROM (SELECT empno, ename
         FROM emp
        WHERE ename like '%'
       ORDER BY DBMS_RANDOM.VALUE)
WHERE rownum <= 1;
     EMPNO ENAME
---------- ----------
      7566 JONES
If you need two employees, use:
SELECT *
 FROM (SELECT empno, ename
         FROM emp
        WHERE ename like '%'
       ORDER BY DBMS_RANDOM.VALUE)
WHERE rownum <= 2;
     EMPNO ENAME
---------- ----------
      7499 ALLEN
      7844 TURNER

No comments:

Post a Comment