- 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 display 3 random values from MySQL table?
Use RAND() for random, whereas LIMIT 3 for the number of values i.e. 3 here −
select yourColumnName from yourTableName order by rand() limit 3;
Let us first create a table −
mysql> create table DemoTable646 ( Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,FirstName varchar(100) ); Query OK, 0 rows affected (0.76 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable646(FirstName) values('John'); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable646(FirstName) values('Bob'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable646(FirstName) values('Chris'); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable646(FirstName) values('David'); Query OK, 1 row affected (0.28 sec) mysql> insert into DemoTable646(FirstName) values('Bob'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable646(FirstName) values('Carol'); Query OK, 1 row affected (0.27 sec) mysql> insert into DemoTable646(FirstName) values('Mike'); Query OK, 1 row affected (0.10 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable646;
This will produce the following output −
+----+-----------+ | Id | FirstName | +----+-----------+ | 1 | John | | 2 | Bob | | 3 | Chris | | 4 | David | | 5 | Bob | | 6 | Carol | | 7 | Mike | +----+-----------+ 7 rows in set (0.00 sec)
Following is the query to display 3 random values from MySQL table −
mysql> select FirstName from DemoTable646 order by rand() limit 3;
This will produce the following output −
+-----------+ | FirstName | +-----------+ | Mike | | John | | Chris | +-----------+ 3 rows in set (0.00 sec)
- Related Articles
- Display random row from a MySQL table
- Display MySQL table values using Java
- Fetch random rows from a table with MySQL
- How to display the column names from a table excluding some in MySQL?
- Updating a MySQL table with values from another table?
- How to display MySQL Table Name with columns?
- How to remove duplicate values from a MySQL table using LEFT JOIN?
- Display table records from a stored procedure in MySQL
- How to display the Engine of a MySQL table?
- Insert values in a table by MySQL SELECT from another table in MySQL?
- Select a fixed number of random records from a MySQL table?
- How to delete the duplicate values stored in reverse order from MySQL table?
- How to display column values as CSV in MySQL?
- How to fetch random rows in MySQL with comma separated values?
- How do I select data from one table only where column values from that table match the column values of another table in MySQL?

Advertisements