Open In App

How to Select Random Row in MySQL

Last Updated : 21 Jun, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

In database operations, selecting random rows from a table is a common requirement for various applications, such as gaming, content recommendation, and statistical sampling.

In this article, we learn different methods for selecting random rows in MySQL. We'll understand various approaches, including the use of RAND(), ORDER BY with LIMIT through multiple examples, and so on.

How to Select Random Row in MySQL

To select a random row in MySQL, you can use the RAND() function with ORDER BY and LIMIT. This approach orders the rows by a random value and retrieves a specified number of random rows.

Using RAND()

RAND() is a function in MySQL that generates a random float value between 0 and 1. This value can be used to order rows randomly when combined with the 'ORDER BY' clause. By limiting the results with the LIMIT clause, you can retrieve a specified number of random rows from a table.

Setting Up the Environment

We will create an employee table and then we will insert at least 10 entries in the table. Let's create a simple employee table having Emp_Id as primary key, apart from primary key we have other columns i.e. Name and Department. We used not null in the name and the department field.

The table that has been created is shown below:

Data-insertion
Shows entries in the EMPLOYEE table

Using RAND() with ORDER BY and LIMIT

RAND() is used to generate a random float value between 0 and 1, and then it is arranged in order by using 'ORDER BY'. The 'LIMIT' can be changed from 1 to the number of entries in the database. If we set the limit to 1, it reflects only one row, and if we set the limit to 2, it will return 2 random rows, and so on.

Example 1: Selecting One Random Row

Query:

SELECT * FROM EMPLOYEE ORDER BY RAND( ) LIMIT 1;

Output:

method2_1
uses of rand() with limit 2

Explanation: The above image displays only one row on the screen as it has been given the Limit of 1. The below image shows two rows on the screen as it has been given only two rows.

Example 2: Selecting Two Random Rows

Query:

SELECT * FROM EMPLOYEE ORDER BY RAND( ) LIMIT 2;

Output:

Rand2
uses of rand() with limit 2

Explanation: The below image has been given the limits of 5 so it reflects five rows on the console. Limited should be smaller than the no. of entries in the table.

Using a Condition with RAND()

This method is used to select a row based on the condition that 'Emp_id' should be greater than the multiplication of the maximum Emp_Id and random value generated by the RAND() function.

Example 1: Selecting One Random Row Based on Condition

Query:

SELECT * FROM EMPLOYEE  WHERE Emp_Id >= RAND() * (SELECT MAX(Emp_Id) FROM EMPLOYEE) ORDER BY Emp_Id LIMIT 1;

Output:

method2_1
WHERE Emp_Id >= RAND() * (SELECT MAX(Emp_Id) FROM EMPLOYEE) limit 1

Explanation: "WHERE Emp_Id >= RAND() * (SELECT MAX(Emp_Id) FROM EMPLOYEE)" is used to select a row based on the condition that Emp_id should be greater than the multiplication of maximum Emp_Id and random value generated by RAND() function. The random function generate floating number between 0 and 1. "ORDER BY" is used to arrange the row in ascending order and limit is used to limited number of rows. The value of limit can from 1 to no. of entries in the database.

Example 2: Selecting Five Random Rows Based on Condition

Query:

SELECT * FROM EMPLOYEE  WHERE Emp_Id >= RAND() * (SELECT MAX(Emp_Id) FROM EMPLOYEE) ORDER BY Emp_Id LIMIT 5;

Output:

method2_2
WHERE Emp_Id >= RAND() * (SELECT MAX(Emp_Id) FROM EMPLOYEE) limit 5

Explanation: The above queries will display two random rowsas it has been given the limit of 5.

Using a SubQuery with RAND()

It is a subquery-based query that is used to generate a random float value using RAND() function, then multiplied by the max 'Emp_Id' in the table and takes the ceil value of the multiplied result.

Example 1: Selecting One Random Row Using SubQuery

Query:

SELECT e.*
FROM EMPLOYEE e
JOIN (
SELECT CEIL(RAND() * (SELECT MAX(Emp_Id) FROM EMPLOYEE)) AS rand_id
) r ON e.Emp_Id >= r.rand_id
ORDER BY e.Emp_Id
LIMIT 1;

Output:

method3_1
Mthod 3 using limit 1

Explanation: The above image display only one row as it has been the limit of 1.

Example 2: Selecting Two Random Rows Using SubQuery

Query:

SELECT e.*
FROM EMPLOYEE e
JOIN (
SELECT CEIL(RAND() * (SELECT MAX(Emp_Id) FROM EMPLOYEE)) AS rand_id
) r ON e.Emp_Id >= r.rand_id
ORDER BY e.Emp_Id
LIMIT 2;

Output:

method3_2
Mthod 3 using limit 4

Explanation: The above image shows the two row random rows of the employee table.

Example 3: Selecting Two Random Rows Using SubQuery

Query:

SELECT e.*
FROM EMPLOYEE e
JOIN (
SELECT CEIL(RAND() * (SELECT MAX(Emp_Id) FROM EMPLOYEE)) AS rand_id
) r ON e.Emp_Id >= r.rand_id
ORDER BY e.Emp_Id
LIMIT 4;

Output:

method3_3
Method 3 using limit 4

Explanation: In the above query we have fetch a random set of records from the EMPLOYEE table by generating a random Emp_Id within the range of existing IDs. It joins the EMPLOYEE table with a subquery that generates a random ID, filters records based on this random ID, orders them by Emp_Id, and limits the output to four random records.

Conclusion

There are multiple ways to select random rows in MySQL. We can use the RAND() function directly, apply conditions based on random values, or utilize subqueries. The LIMIT clause plays a crucial role in restricting the number of rows returned. The value of LIMIT can range from 1 to the total number of rows in the table. By using these methods, you can effectively retrieve random rows for various applications.

FAQs: Selecting Random Rows in MySQL

What is the purpose of selecting random rows in MySQL?

Selecting random rows is useful for applications such as gaming, content recommendations, statistical sampling, and other scenarios where you need a random subset of data from a table.

What is the RAND() function?

The RAND() function generates a random floating-point value between 0 and 1. It can be used to introduce randomness in queries.

Can I use RAND() with other MySQL functions?

Yes, RAND() can be used with other MySQL functions to introduce randomness in various parts of your query, such as in the WHERE clause, JOIN conditions, or even within aggregate functions.


Next Article

Similar Reads