0
0
PostgreSQLquery~5 mins

TABLESAMPLE for random sampling in PostgreSQL - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does the TABLESAMPLE clause do in PostgreSQL?
It allows you to retrieve a random sample of rows from a table instead of the entire table, which is useful for quick analysis or testing.
Click to reveal answer
beginner
How do you specify the size of the sample when using TABLESAMPLE?
You specify the sample size as a percentage depending on the sampling method, for example: TABLESAMPLE SYSTEM (10) samples approximately 10% of the table.
Click to reveal answer
intermediate
What are the two main sampling methods supported by PostgreSQL's TABLESAMPLE?
The two main methods are SYSTEM and BERNOULLI. SYSTEM samples pages, while BERNOULLI samples rows individually.
Click to reveal answer
intermediate
True or False: Using TABLESAMPLE SYSTEM guarantees the exact percentage of rows sampled.
False. SYSTEM sampling works at the page level, so the exact number of rows sampled can vary and is approximate.
Click to reveal answer
beginner
Write a simple query to select approximately 5% random rows from a table named employees using TABLESAMPLE.
SELECT * FROM employees TABLESAMPLE SYSTEM (5);
Click to reveal answer
Which TABLESAMPLE method samples rows individually in PostgreSQL?
ABERNOULLI
BSYSTEM
CRANDOM
DPAGE
What does TABLESAMPLE SYSTEM (10) do?
ASamples exactly 10 rows
BSamples approximately 10% of the table's rows
CSamples 10 random columns
DSamples 10 random tables
Can TABLESAMPLE guarantee the exact number of rows returned?
AYes, always
BOnly with BERNOULLI
COnly with SYSTEM
DNo, it returns an approximate sample
Which of the following is a valid TABLESAMPLE clause in PostgreSQL?
ATABLESAMPLE SYSTEM (20)
BTABLESAMPLE RANDOM (20)
CTABLESAMPLE ROWS (20)
DTABLESAMPLE PERCENT (20)
Why might you use TABLESAMPLE in a query?
ATo update all rows in a table
BTo sort data alphabetically
CTo speed up queries by working with a smaller random subset
DTo create a new table
Explain how TABLESAMPLE works in PostgreSQL and the difference between SYSTEM and BERNOULLI methods.
Think about how data is stored in pages and how sampling can happen at different levels.
You got /4 concepts.
    Describe a practical scenario where using TABLESAMPLE would be helpful.
    Imagine you want to test a query or get a quick insight without scanning the whole table.
    You got /4 concepts.