How to use the RAND() Function In MySQL
The simplest the easiest 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 us to sort and limit the selection.
Syntax:
SELECT * FROM Table name
ORDER BY RAND()
LIMIT 10;
Example: Select RAND() for Employee Table
Query:
SELECT * FROM Employee
ORDER BY RAND()
LIMIT 10;
This query selects 10 random rows from EMPLOYEE. The ORDER BY RAND() clause randomizes the row order, and the LIMIT 10 clause restricts the output to 10 rows.
Output:
Explanation: The SQL query selects 10 random rows from the Employee table and orders them randomly. The output displays those 10 rows in a random order.
How to Select 10 Random Rows from 600K Rows Fast in MySQL?
Selecting random rows simultaneously from a database is a common task in SQL especially when handling large datasets. Selecting multiple rows is useful for sampling data or generating random subsets for analysis. In MySQL, this can be achieved using various methods, each has its advantages.
In this article, we explore retrieving 10 random rows from a dataset of 600k rows in MySQL using three methods. We present their syntax, examples, and explanations, providing insights into efficient random row selection.
Contact Us