Explain TABLESAMPLE and other approaches to random sampling in SQL. When and why would you sample, and what are the trade-offs?

Last updated 13 July 2026

  • TABLESAMPLE returns a random subset fast: BERNOULLI samples individual rows fairly but scans everything, while SYSTEM samples whole pages (faster but biased if data is ordered on disk). REPEATABLE(seed) makes it reproducible.

  • Alternatives: ORDER BY RANDOM() LIMIT n gives exact n rows but sorts the whole table. A hash filter like MOD(HASH(id), 100) = 0 gives a stable sample across runs and joins.

  • Sample for quick exploration, approximate aggregates, or dev datasets. Accept sampling error and scale counts by the sampling fraction.

  • https://www.sqlservercentral.com/articles/from-rows-to-pages-the-hidden-chaos-behind-sql-servers-sampling-methods
  • https://www.ibm.com/docs/en/db2/12.1.x?topic=design-data-sampling-in-queries
All Advanced SQL interview questions