- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to retrieve a random row or multiple random rows in MySQL?
You can use RAND() method for this. To retrieve a random row, use the following syntax
SELECT *FROM yourTableName ORDER BY RAND() LIMIT yourIntegerNumber;
To understand the above syntax, let us create a table. The query to create a table is as follows
mysql> create table gettingRandomRow -> ( -> CustomerId int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> CustomerName varchar(100) -> ); Query OK, 0 rows affected (0.45 sec)
Insert some records in the table using insert command. The query is as follows −
mysql> insert into gettingRandomRow(CustomerName) values('Chris'); Query OK, 1 row affected (0.14 sec) mysql> insert into gettingRandomRow(CustomerName) values('Robert'); Query OK, 1 row affected (0.10 sec) mysql> insert into gettingRandomRow(CustomerName) values('Ramit'); Query OK, 1 row affected (0.15 sec) mysql> insert into gettingRandomRow(CustomerName) values('James'); Query OK, 1 row affected (0.11 sec) mysql> insert into gettingRandomRow(CustomerName) values('Jace'); Query OK, 1 row affected (0.12 sec) mysql> insert into gettingRandomRow(CustomerName) values('Mike'); Query OK, 1 row affected (0.15 sec) mysql> insert into gettingRandomRow(CustomerName) values('Sam'); Query OK, 1 row affected (0.17 sec) mysql> insert into gettingRandomRow(CustomerName) values('Bob'); Query OK, 1 row affected (0.12 sec) mysql> insert into gettingRandomRow(CustomerName) values('Carol'); Query OK, 1 row affected (0.11 sec) mysql> insert into gettingRandomRow(CustomerName) values('David'); Query OK, 1 row affected (0.16 sec) mysql> insert into gettingRandomRow(CustomerName) values('Maxwell'); Query OK, 1 row affected (0.14 sec) mysql> insert into gettingRandomRow(CustomerName) values('Larry'); Query OK, 1 row affected (0.13 sec) mysql> insert into gettingRandomRow(CustomerName) values('John'); Query OK, 1 row affected (0.11 sec)
Display all records from the table using select statement. The query is as follows −
mysql> select *from gettingRandomRow;
The following is the output
+------------+--------------+ | CustomerId | CustomerName | +------------+--------------+ | 1 | Chris | | 2 | Robert | | 3 | Ramit | | 4 | James | | 5 | Jace | | 6 | Mike | | 7 | Sam | | 8 | Bob | | 9 | Carol | | 10 | David | | 11 | Maxwell | | 12 | Larry | | 13 | John | +------------+--------------+ 13 rows in set (0.00 sec)
Here is the query to get a random row or multiple random rows.
Case 1: The query is as follows −
mysql> select *from gettingRandomRow order by rand() LIMIT 1;
The following is the output
+------------+--------------+ | CustomerId | CustomerName | +------------+--------------+ | 13 | John | +------------+--------------+ 1 row in set (0.00 sec)
Case 2: The query is as follows −
mysql> select *from gettingRandomRow order by rand() LIMIT 2;
The following is the output
+------------+--------------+ | CustomerId | CustomerName | +------------+--------------+ | 2 | Robert | | 3 | Ramit | +------------+--------------+ 2 rows in set (0.00 sec)
Case 3: The query is as follows −
mysql> select *from gettingRandomRow order by rand() LIMIT 2;
The following is the output
+------------+--------------+ | CustomerId | CustomerName | +------------+--------------+ | 4 | James | | 9 | Carol | +------------+--------------+ 2 rows in set (0.00 sec)
- Related Articles
- Select a random row in MySQL
- Display random row from a MySQL table
- Select two random rows in a MySQL database?
- MySQL query to select a random row value (Id and Name) having multiple occurrences (Name)?
- Select random row that exists in a MySQL table?
- Fetch random rows from a table with MySQL
- How to fetch random rows in MySQL with comma separated values?
- How to order results of a query randomly & select random rows in MySQL?
- MySQL query to select one specific row and another random row?
- MySQL random rows sorted by a specific column name?
- How do I select 5 random rows from the 20 most recent rows in MySQL?
- Concatenate multiple rows and columns in a single row with MySQL
- How to get MySQL random integer range?
- Select only 5 random rows in the last 50 entries With MySQL?
- What is the most efficient way to select a specific number of random rows in MySQL?

Advertisements