0
0
PostgreSQLquery~20 mins

TABLESAMPLE for random sampling in PostgreSQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
TABLESAMPLE Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
What does this TABLESAMPLE query return?
Consider a table employees with 1000 rows. What is the expected output of this query?
SELECT * FROM employees TABLESAMPLE SYSTEM (10);
PostgreSQL
SELECT * FROM employees TABLESAMPLE SYSTEM (10);
ASyntax error due to incorrect TABLESAMPLE usage
BExactly 10 rows randomly sampled from employees
CAll rows where a column named SYSTEM equals 10
DApproximately 10% of rows randomly sampled from employees
Attempts:
2 left
💡 Hint
SYSTEM sampling method picks a percentage of rows approximately.
📝 Syntax
intermediate
2:00remaining
Which option is the correct syntax for TABLESAMPLE in PostgreSQL?
Identify the valid TABLESAMPLE syntax to sample 5% of rows from a table named orders.
ASELECT * FROM orders TABLESAMPLE (5) BERNOULLI;
BSELECT * FROM orders TABLESAMPLE BERNOULLI 5%;
CSELECT * FROM orders TABLESAMPLE BERNOULLI (5);
DSELECT * FROM orders TABLESAMPLE SYSTEM 5;
Attempts:
2 left
💡 Hint
The sampling method name comes right after TABLESAMPLE, followed by the percentage in parentheses.
optimization
advanced
2:00remaining
Which TABLESAMPLE method is more efficient for large tables and why?
You want to sample 1% of rows from a very large table. Which TABLESAMPLE method is generally more efficient and why?
ASYSTEM, because it samples data blocks instead of individual rows
BSYSTEM, because it guarantees exact percentage of rows sampled
CBERNOULLI, because it uses indexes to speed up sampling
DBERNOULLI, because it checks each row individually for sampling
Attempts:
2 left
💡 Hint
Think about how sampling by blocks vs rows affects performance.
🧠 Conceptual
advanced
2:00remaining
Why might TABLESAMPLE SYSTEM not return exactly the requested percentage of rows?
Explain why TABLESAMPLE SYSTEM (10) might return more or fewer than exactly 10% of rows.
ABecause SYSTEM samples entire data blocks, and block sizes vary, so exact row count varies
BBecause SYSTEM filters rows based on a hidden column, causing variation
CBecause SYSTEM uses a random seed that changes the percentage each time
DBecause SYSTEM rounds the percentage to the nearest integer
Attempts:
2 left
💡 Hint
Think about how data is stored physically in blocks.
🔧 Debug
expert
2:00remaining
What error does this query raise and why?
Given the query:
SELECT * FROM sales TABLESAMPLE BERNOULLI (105);

What error will PostgreSQL raise and why?
PostgreSQL
SELECT * FROM sales TABLESAMPLE BERNOULLI (105);
AERROR: syntax error near 'BERNOULLI'
BERROR: sampling percentage must be between 0 and 100
CNo error, returns all rows because percentage > 100
DERROR: table 'sales' does not exist
Attempts:
2 left
💡 Hint
Sampling percentage must be a valid percentage value.