SELECT N Percent of Records in MySQL

The ability to select a certain percentage of records from a query or database table can be important when working with lots of data. Luckily this is easy to do in MySQL with the RAND() function and a WHERE clause.

RAND() returns a random floating point number between 0 and 1, making it easy to select a certain percentage of all records returned from a query. For example, if you want to select approximately 5% of all records, use WHERE RAND() < 0.05. For approximately 25% of records use WHERE RAND() < 0.25, and so on.

Here is an example of selecting 1% of all records from the my_data table. This table contains 1000 records, so this query should return approximately 10.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">