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.

This was last published in September 2002

Dig Deeper on Oracle and SQL



Find more PRO+ content and other member only offers, here.

Have a question for an expert?

Please add a title for your question

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.

By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Please create a username to comment.