Using TABLESAMPLE for Random Sampling in PostgreSQL
📖 Scenario: You work for a bookstore that wants to analyze a small random sample of its sales data to understand customer preferences without scanning the entire large sales table.
🎯 Goal: Build a SQL query using TABLESAMPLE SYSTEM to randomly select approximately 10% of the rows from the sales table.
📋 What You'll Learn
Create a
sales table with columns sale_id, book_title, and price.Insert exactly 5 rows into the
sales table with specified values.Write a query using
TABLESAMPLE SYSTEM (10) to randomly sample about 10% of the rows.Add an
ORDER BY sale_id clause to the sampling query.💡 Why This Matters
🌍 Real World
Random sampling helps businesses analyze a manageable subset of large data quickly, saving time and resources.
💼 Career
Data analysts and database administrators often use TABLESAMPLE to perform quick exploratory data analysis on big datasets.
Progress0 / 4 steps