Open In App

Return Random Row From a Table in MariaDB

Last Updated : 13 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

In database management accessing random rows from a table can be a valuable functionality for various applications. By using techniques like ORDER BY RAND() or selecting a random offset with LIMIT users can access random rows efficiently. In this article, We will learn about How to Return a Random Row from a Table in MariaDB with the help of various examples and so on.

Introduction to RAND() Function

The RAND() function is widely used in MariaDB to generate a random floating-point value between 0 and 1. RAND() can shuffle the order of result sets which is helpful for applications like displaying random rows from the table.

Syntax:

To get a random row from a table in MariaDB by using the ORDER BY RAND() clause. Combine it with the LIMIT clause. Here’s the syntax:

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

Examples of Return Random Row from a Table in MariaDB

we have a table named students with columns id, name, age, grade, and joining. Then we’ll insert some data into the table and perform some queries.

CREATE TABLE students 
(
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(50),
age INT,
grade VARCHAR(2),
joining DATE
);
SELECT * FROM students;

Ouput:

MariaDbRandomRow1

all data

Example 1

Let’s fetch the random row from the students and restrict the rows to fetched only one row with the help of RAND() and LIMIT Function.

Query:

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

Output:

MariaDbRandomRow2

any 1 random

Explanation: In the above query, We have fetched a random row from the “students” table by first sorting the rows randomly using the RAND() function and then limiting the result set to only one row with LIMIT 1.

Example 2

Query:

SELECT * FROM students WHERE age >= 18 
ORDER BY RAND() LIMIT 1;

Output:

MariaDbRandomRow3

any 1 random by age

Explanation: In the above query, We have fetched a random record from the students table where the age is 18 or older. In this query it first filters the rows based on the condition age >= 18 then set the result randomly using ORDER BY RAND() and finally limits the output to only one row using LIMIT 1.

Example 3

Query:

SELECT * FROM students 
WHERE joining BETWEEN '2023-01-01' AND '2023-06-30'
ORDER BY RAND() LIMIT 1;

Output:

MariaDbRandomRow4

any 1 random between date

Explanation: In the above query, We have fetched a random student who joined between Feb 1, 2023, and June 30, 2023 and a random record from the students table where the joining date lie between January 1, 2023, and June 30, 2023. Then set the result set randomly using ORDER BY RAND() and finally limits the output to just one row using LIMIT 1.

Conclusion

In MariaDB, you can select a random row from a table. You use­ the ORDER BY RAND() with the LIMIT clause. This me­thod gets a single random record. It’s easy for choosing data in database queries. Adding this me­thod to your queries lets you bring in randomne­ss.


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads