Q
Problem solve Get help with specific problems with your technologies, process and projects.

# Selecting every 100th of 1,000,000 rows

How can I select every 100th record for a table of 1,000,000 rows? For example, the table consists of a distinct memberid, sex, and age, and I only want every 100th member from this table.

There's really no perfect way to do this with just SQL. The best solution is to use an extension to SQL like Transact-SQL (Sybase and Microsoft SQL/Server) or PL/SQL (Oracle). These language extensions allow you to write processing logic to cursor through the table, fetching every 100th row using a loop counter. This is not only the most accurate method but the most efficient as well.

If your table's memberid is an auto_increment or identity or sequence column, and if it has relatively few gaps in the numeric sequence, then you can come close to what you want with the following query --

``` select memberid, sex, age from yourtable where mod(memberid+27,100) = 0
```

Here mod() is the modulus function, which returns a remainder upon division. Check your database for the specific function name. I added 27 to the memberid simply to introduce some randomness. The query will select memberids 73, 173, 273, and so on, if they exist. Out of a million rows, this should yield ten thousand, fewer if there are gaps.

You see, a lot depends on what you want these rows for. If you need to do a strict statistical calculation, and need exactly every 100th row, you would want the first solution. If you're just interested in getting approximately one out of every hundred rows, more or less, for testing purposes, then the modulus approach will usually suffice.

Finally, a recommendation I made in a previous answer, Selecting random rows (29 May 2001):

The best solution, for situations where you only need a small number of rows, such as in testing, is to have a sample table which contains not random rows of live data, but test cases -- in other words, a place where you can collect those rows which are of interest or which represent conditions you want to test for.

#### Have a question for an expert?

Get answers from a TechTarget expert on whatever's puzzling you.

You will be able to add details on the next page.

#### Start the conversation

Send me notifications when other members comment.

## SearchDataManagement

• ### PostgreSQL 12 boosts open source database performance

Widely used open source PostgreSQL database platform gets a major update providing users with new SQL query capabilities for JSON...

• ### Data integration vs. ETL in the age of big data

Data integration provides a consistent view of business performance across multiple data sources, though it needs to keep up with...

• ### 5 steps to an improved data quality assurance plan

Consultant David Loshin offers tips on developing a data quality strategy that can help identify data errors before they cause ...

• ### ThoughtSpot BI platform an early adopter of AI

Due to AI, ThoughtSpot's analytics tools have been accessible to citizen data scientists from the start, Andrew Yeung, the ...

• ### ThoughtSpot 6 advances AI power of BI platform

New augmented intelligence and machine learning tools, along with a new mobile app, highlight the release of the new update to ...

• ### Promethium tool taps natural language processing for analytics

The software, called Data Navigation System, was designed to enable non-technical users to make complex SQL requests using plain ...

## SearchSAP

• ### CEO of SAP Bill McDermott abruptly resigns

Bill McDermott, CEO of longtime ERP, CRM and BI software vendor SAP, resigned Thursday triggering a succession plan that promotes...

• ### What is an SAP Intelligent Enterprise? Don't look for a clear answer

What does Intelligent Enterprise actually mean? If the SAP term confuses you, you're not alone. Learn what the experts say and ...

• ### Pros and cons of SAP migration to the cloud

Hosting SAP on cloud services can bring added security and scalability. But organizations must also watch out for issues such as ...

## SearchSQLServer

• ### SQL Server database design best practices and tips for DBAs

Good database design is a must to meet processing needs in SQL Server systems. In a webinar, consultant Koen Verbeeck offered ...

• ### SQL Server in Azure database choices and what they offer users

SQL Server databases can be moved to the Azure cloud in several different ways. Here's what you'll get from each of the options ...

• ### Using a LEFT OUTER JOIN vs. RIGHT OUTER JOIN in SQL

In this book excerpt, you'll learn LEFT OUTER JOIN vs. RIGHT OUTER JOIN techniques and find various examples for creating SQL ...

## TheServerSide.com

• ### Java video games, like Minecraft, keep the language popular

The next generation of Java developers can learn the ins and outs of the language through an unlikely source: video games such as...

• ### Pivotal, Microsoft team up to deliver Azure Spring Cloud

Azure Spring Cloud, jointly developed by Microsoft and Pivotal, lets Spring developers bring apps to the cloud without concern ...

• ### GitHub tempts enterprises with Semmle, security enhancements

With the Semmle semantic code analysis engine freshly added to its quiver, GitHub gives corporate development teams one way to ...

## SearchDataCenter

• ### Essential private cloud migration steps

Executive buy-in, application evaluation and tool configuration are all important parts of cloud selection. With these steps, ...

• ### IBM quantum computers' usefulness in sight -- using binoculars

IBM's Bob Sutor discusses Big Blue's new quantum systems and computation center, the realities of quantum computing today and how...

• ### Addendum sets ASHRAE 90.4 as energy-efficiency standard

The publication of ASHRAE 90.4 in 2016 brought a new set of energy guidelines, but the industry still used Standard 90.1. A new ...

## SearchContentManagement

• ### Digital asset management benefits the future of work

Digital asset management promises to transform digital work, making it easier to tag documents, analyze video and provide image ...

• ### Box cloud content management adds PDF tools, boosts security

Box partners with Adobe in releasing a new set of PDF tools that can be used within Box's content management cloud to enable ...

• ### AI optimizes content management workflows, personalizes CX

AI is poised to revolutionize content management workflows, bringing order to enterprise documents, greater insights into ...

## SearchHRSoftware

• ### Get smarter about HR data security

HR deals with mountains of sensitive employee data and must do a better job of protecting that information. Here's a look at how ...

• ### Walmart, FedEx make case for VR training

The payback from virtual training may be improved retention and shorter training times. An hour-long training period can be ...

• ### Do you know how to improve employee experience? Test yourself

Think you know what it takes to create a positive employee experience for your workers? Take this EX quiz to test your savvy and ...

Close