
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
Select a random row in MySQL
To select a random row, use the rand() with LIMIT from MySQL. The syntax is as follows:
SELECT * FROM yourTableName ORDER BY RAND() LIMIT 1;
To understand the above syntax, let us create a table. The query to create a table is as follows:
mysql> create table generateRandomRow -> ( -> Id int NOT NULL AUTO_INCREMENT, -> Name varchar(20), -> Age int, -> PRIMARY KEY(Id) -> ); Query OK, 0 rows affected (1.66 sec)
Insert some records in the table using insert command. The query is as follows:
mysql> insert into generateRandomRow(Name,Age) values('John',23); Query OK, 1 row affected (0.22 sec) mysql> insert into generateRandomRow(Name,Age) values('Larry',21); Query OK, 1 row affected (0.32 sec) mysql> insert into generateRandomRow(Name,Age) values('David',21); Query OK, 1 row affected (0.39 sec) mysql> insert into generateRandomRow(Name,Age) values('Carol',24); Query OK, 1 row affected (0.21 sec) mysql> insert into generateRandomRow(Name,Age) values('Bob',27); Query OK, 1 row affected (0.14 sec) mysql> insert into generateRandomRow(Name,Age) values('Mike',29); Query OK, 1 row affected (0.19 sec) mysql> insert into generateRandomRow(Name,Age) values('Sam',26); Query OK, 1 row affected (0.17 sec)
Display all records from the table using select statement. The query is as follows:
mysql> select *from generateRandomRow;
The following is the output:
+----+-------+------+ | Id | Name | Age | +----+-------+------+ | 1 | John | 23 | | 2 | Larry | 21 | | 3 | David | 21 | | 4 | Carol | 24 | | 5 | Bob | 27 | | 6 | Mike | 29 | | 7 | Sam | 26 | +----+-------+------+ 7 rows in set (0.00 sec)
Here is the query to select a random row from the table using rand(). The query is as follows:
mysql> select *from generateRandomRow order by rand() limit 1;
The output displays a random row:
+----+------+------+ | Id | Name | Age | +----+------+------+ | 5 | Bob | 27 | +----+------+------+ 1 row in set (0.00 sec)
Now when we will run the query again, the following random row would be visible:
mysql> select *from generateRandomRow order by rand() limit 1;
The following is the output:
+----+------+------+ | Id | Name | Age | +----+------+------+ | 6 | Mike | 29 | +----+------+------+ 1 row in set (0.00 sec)
- Related Articles
- Select random row that exists in a MySQL table?
- MySQL query to select one specific row and another random row?\n
- MySQL query to select a random row value (Id and Name) having multiple occurrences (Name)?
- Display random row from a MySQL table
- Select two random rows in a MySQL database?
- How to retrieve a random row or multiple random rows in MySQL?
- MySQL LIMIT to select a single row
- Select random number from a specific list in MySQL?
- How to select last row in MySQL?
- SELECT a row by subtracting dates in WHERE in MySQL?
- How to select a random record from a MySQL database?
- Write a Python code to select any one random row from a given DataFrame
- SELECT where row value contains string in MySQL?
- How to select next row pagination in MySQL?
- Select a fixed number of random records from a MySQL table?

Advertisements