OraQA

Oracle Question and Answer

  • Do you have a solution to a problem? Do you have an unanswered question? Login and share it with the Oracle community. More...

Oracle News


Entries RSS feed

Comments RSS feed

How to return rows in a random order

January 27th, 2006 By Eddie Awad

To make sure that rows are returned in no specific order, order by a random value. For example:

HR@XE> select *
  2  from (
  3      select first_name
  4      from employees
  5      order by dbms_random.value()
  6      )
  7  where rownum <= 3
  8  /

FIRST_NAME
--------------------
Jack
Randall
Harrison

HR@XE> /

FIRST_NAME
--------------------
Anthony
Vance
Michael

2 Responses to “How to return rows in a random order”

  1. Claudiu Ariton Says:

    Another way to return a sample set of data (procentual) is

    select * from TABLE sample (10)
    select * from TABLE sample (0.1)

    Best regards,
    Claudiu Ariton

  2. Francois Degrelle Says:

    With Oracle 10g, you have also the ORA_HASH() function

    SELECT * FROM sales
       WHERE ORA_HASH(cust_id, 9) = 1;
    
       PROD_ID    CUST_ID TIME_ID   C   PROMO_ID QUANTITY_SOLD AMOUNT_SOLD
    ---------- ---------- --------- - ---------- ------------- -----------
          2510       6950 01-FEB-98 S       9999             2          78
          9845       9700 04-FEB-98 C       9999            17         561
          3445      33530 07-FEB-98 T       9999             2         170
    . . .
           740      22200 13-NOV-00 S       9999             4         156
          9425       4750 29-NOV-00 I       9999            11         979
          1675      46750 29-NOV-00 S       9999            19        1121
    97 rows selected.
    

    ORA_HASH documentation:
    http://download-west.oracle.com/docs/cd/B14117_01/server.101/b10759/functions097.htm#SQLRF06313

Leave a Reply

You must be logged in to post a comment.

RSS feed for comments on this question