Return a random sample of rows from a table efficiently, without scanning and sorting the full table.
Returns a statistical sample of rows from a table using BERNOULLI (row-level random) or SYSTEM (block-level random) methods. Useful for quick approximate counts and sampling large tables. The sample size is approximate, not exact.
| Database System | Support Status | Since Version | Notes |
|---|---|---|---|
| MySQL | ✗ Not Supported | — | Not supported. ORDER BY RAND() LIMIT n works but scans the full table and is very slow on large datasets. |
| PostgreSQL | ✓ Supported | 9.5 | Two methods: BERNOULLI (true row-level statistical sample — accurate but slower) and SYSTEM (page-level — faster but may have clustering bias). Optional REPEATABLE(seed) for reproducible results. Custom sampling methods can be added via extensions (e.g., tsm_system_rows). |
| SQL Server | ✓ Supported | 2005 | TABLESAMPLE SYSTEM(n PERCENT) or TABLESAMPLE (n ROWS). Only the SYSTEM method (page-level). Optional REPEATABLE(seed). |
| Oracle | ✓ Supported | 8i | Non-standard SAMPLE clause syntax (not called TABLESAMPLE). SAMPLE(pct) is row-level (like BERNOULLI); SAMPLE BLOCK(pct) is page-level (like SYSTEM). Optional SEED(n) for reproducibility. |
| SQLite | ✗ Not Supported | — | Not supported. Workaround: random rowid selection. |
PostgreSQL is the only engine offering both BERNOULLI (true row-level) and SYSTEM (page-level) methods in standard SQL. SYSTEM is faster but results can be biased toward rows that happen to share a page. Oracle uses non-standard SAMPLE syntax.
Not supported. ORDER BY RAND() LIMIT n works but scans the full table and is very slow on large datasets.
Two methods: BERNOULLI (true row-level statistical sample — accurate but slower) and SYSTEM (page-level — faster but may have clustering bias). Optional REPEATABLE(seed) for reproducible results. Custom sampling methods can be added via extensions (e.g., tsm_system_rows).
TABLESAMPLE SYSTEM(n PERCENT) or TABLESAMPLE (n ROWS). Only the SYSTEM method (page-level). Optional REPEATABLE(seed).
Non-standard SAMPLE clause syntax (not called TABLESAMPLE). SAMPLE(pct) is row-level (like BERNOULLI); SAMPLE BLOCK(pct) is page-level (like SYSTEM). Optional SEED(n) for reproducibility.
Not supported. Workaround: random rowid selection.