Search notes:

Oracle: SELECT a random SAMPLE from a table

select … from TAB SAMPLE [BLOCK] (PERCENTAGE [, GROUP-SIZE]);
Select approximately 0.1 percent of the records found in xyz:
select
   *
from
   xyz
SAMPLE(0.1)
In order to select the same sample twice (assuming that the records didn't change), the sample clause can be combined with a seed:
select
   *
from
   xyz
SAMPLE(0.1) SEED(42)
The sample clause logically belongs to the table (or more accurately to the query table expression) so that where clauses are applied after determining the sample.

Execution plan

In an execution plan, a sample access to table is identified by a TABLE ACCESS (SAMPLE) row source:
create table tq84_smp (
    id  number,
    val number,
    txt varchar2(10)
);

insert into tq84_smp
select
   level,
   mod(level*37, 11),
   to_char(1234+mod(level*197, 193), 'fmxxxxx')
from
   dual connect by level <= 1000;

begin
   dbms_stats.gather_table_stats(user, 'tq84_smp');
end;
/
   

explain plan for
   select
      *
   from
      tq84_smp sample(9)
   where
      id < 100
   ;


select * from table(dbms_xplan.display(format=>'basic rows'));
--
-- ------------------------------------------------
-- | Id  | Operation           | Name     | Rows  |
-- ------------------------------------------------
-- |   0 | SELECT STATEMENT    |          |     9 |
-- |   1 |  TABLE ACCESS SAMPLE| TQ84_SMP |     9 |
-- ------------------------------------------------


drop table tq84_smp;

Sampling views using DISTINCT, GROUP BY etc.

select
   *
from
   dba_objects SAMPLE(1);
--
-- ORA-01446: cannot select ROWID from, or sample, a view with DISTINCT, GROUP BY, etc.

See also

Limiting the resultset of a select statement (fetch first rows only or rows between records n and m).
The sample clause implements SQL feature T613 (but uses sample rather than tablesample).
ORA-30562: invalid SAMPLE percentage ORA-30562: invalid SAMPLE percentage
ORA-30577: SAMPLE group size must be in the range [1,UB4MAXVAL]

Index