Understanding Teradata’s SAMPLE Function: Randomly Select Rows and More

What is the Teradata Sample Function?

The SAMPLE function returns a set of randomly selected rows. Here are the characteristics:

  • Ask for a sample with an absolute number of rows
  • Ask for a sample that contains a certain percentage of the table rows 
  • Ask for more than one sample at the same time
  • Include the column SAMPLEID to determine from which sample a row originates
  • Ask for samples with or without duplicates

The Syntax of the Sample Function

SAMPLE [WITH REPLACEMENT] [RANDOMIZED ALLOCATION][WHEN <condition> THEN] {<number of row> | <percentage>} […,<number-of-rows> | <percentage>][ELSE {<number of rows> | <percentage } END]

Here are some examples of how to use the SAMPLE function:

100 Random Rows

SELECT * FROM CustomerSAMPLE 100;

10% of Table Rows

SELECT * FROM CustomerSAMPLE .10;

2 Samples with 10% of Table Rows (no Duplicates)

SELECT * FROM CustomerSAMPLE .1,.1;

2 Samples with 10 Rows and SAMPLEID

SELECT    t01.* ,   SAMPLEIDFROM Customer t01SAMPLE 10,10;

2 Samples with 10% of Table Rows (with Duplicates)

SELECT * FROM CustomerSAMPLE WITH REPLACEMENT .1,.1;

 

2 Samples with Conditional Logic Applied 

SELECT * FROM CustomerSAMPLE WHEN CustomerId > 1000 THEN .1,.1 ELSE .3,.3 END;

Related Services

⚡ Need Help Optimizing Your Data Platform?

We cut data platform costs by 30–60% without hardware changes. 25+ years of hands-on tuning experience.

Explore Our Services →

📋 Considering a Move From Teradata?

Get a personalized migration roadmap in 2 minutes. We have migrated billions of rows from Teradata to Snowflake, Databricks, and more.

Free Migration Assessment →

📊 Data Platform Migration Survey

Help us map where the industry is heading. Results are public — see what others chose.

1. What is your current data platform?

2. Where are you migrating to (or evaluating)?

Migrating FROM
Migrating TO

Thanks for voting! Share this with your network.

Follow me on LinkedIn for daily insights on data warehousing and platform migrations.

Stay Ahead in Data Warehousing

Get expert insights on Teradata, Snowflake, BigQuery, Databricks, Microsoft Fabric, and modern data architecture — delivered to your inbox.

1 thought on “Understanding Teradata’s SAMPLE Function: Randomly Select Rows and More”

  1. hi how can i set a number variable within the function SAMPLE, par exemple : sel * from employeeTable sample N.
    thanks an advance

    Reply

Leave a Comment

DWHPro

Expert network for enterprise data platforms. Senior consultants, project teams built for your challenge — across Teradata, Snowflake, Databricks, and more.

📍Vienna, Austria & Jacksonville, Florida

Quick Links
Services Team Teradata Book Blog Contact Us
Connect
LinkedIn → [email protected]
Newsletter

Join 4,000+ data professionals.
Weekly insights on Teradata, Snowflake & data architecture.