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)

Combining with a WHERE clause

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.
create table tq84_sample (
   id  integer,
   val varchar2(3)
);

insert into tq84_sample
select
   level,
   dbms_random.string('a', 3)
from
   dual connect by level <= 100;
Expect approximately 2 records to be returned:
select * from tq84_sample sample(20) where mod(id, 10) = 0;
Cleaning up:
drop table tq84_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;

Selecting approximately N records

The sample clause only allows to specify an approximate percentage of the returned records, not an approximate amount of records.
The following example demonstrates how it is possible to select approximately n (here: 1234) records from a table:
create table tq84_t (id integer);

insert into tq84_t
select
   dbms_random.value(1, 100000000)
from
   dual connect by level <= 1000000;

select
   count(*)
from
   tq84_t
where
   mod(id, trunc((select count(*) / 1234 from tq84_t))) = 0;

drop table tq84_t;

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

Fatal error: Uncaught PDOException: SQLSTATE[HY000]: General error: 8 attempt to write a readonly database in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php:78 Stack trace: #0 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(78): PDOStatement->execute(Array) #1 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(30): insert_webrequest_('/notes/developm...', 1758199135, '216.73.216.150', 'Mozilla/5.0 App...', NULL) #2 /home/httpd/vhosts/renenyffenegger.ch/httpsdocs/notes/development/databases/Oracle/SQL/select/sample(167): insert_webrequest() #3 {main} thrown in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php on line 78