MySQL Select Random 10 Rows
Robert Cooper
Robert Cooper Senior Engineer at Basedash
· January 31, 2025
Robert Cooper
Robert Cooper Senior Engineer at Basedash
· January 31, 2025
Selecting random rows from a database is a common task in SQL, particularly useful for sampling data or generating random subsets for analysis. In MySQL, this can be achieved using various methods, each with its own set of advantages.
The simplest way to select random rows is by using the RAND() function. This function generates a random floating-point value between 0 and 1 for each row, allowing you to sort and limit your selection.
SELECT * FROM your_table
ORDER BY RAND()
LIMIT 10;
This query selects 10 random rows from your_table. The ORDER BY RAND() clause randomizes the row order, and the LIMIT 10 clause restricts the output to 10 rows.
RAND() is evaluated for every row.If your table has a numeric primary key with relatively few gaps, you can use it for more efficient random selection.
SELECT * FROM your_table
WHERE primary_key_column >= (SELECT FLOOR(MAX(primary_key_column) * RAND()) FROM your_table)
ORDER BY primary_key_column
LIMIT 10;
This query randomly selects a starting point based on the primary key and retrieves the next 10 rows.
RAND() on the entire table.For more control over randomness, especially in large datasets, you can use user variables to assign a random number to each row and then select based on these numbers.
SET @row_number = 0;
SELECT *
FROM (
SELECT *, (@row_number:=@row_number + 1) AS num
FROM your_table
ORDER BY RAND()
) AS t
WHERE num % (SELECT ROUND(COUNT(*) / 10) FROM your_table) = 0
LIMIT 10;
This query first assigns a row number to each row in a random order, then selects rows based on these numbers.
Choosing the right method for selecting random rows in MySQL depends on your specific requirements, such as the size of your dataset and the need for reproducibility or performance. Experiment with different approaches to find the most suitable one for your scenario.
Written by
Senior Engineer at Basedash
Robert Cooper is a senior engineer at Basedash who builds full-stack product systems across SQL data infrastructure, APIs, and frontend architecture. His work focuses on application performance, developer velocity, and reliable self-hosted workflows that make data operations easier for teams at scale.
Basedash lets you build charts, dashboards, and reports in seconds using all your data.