
- 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
In MySQL, how can we randomize set of rows or values in the result set?
With the help of RAND() function used along with the ORDER BY clause, the set of rows or values can be randomized in the result set. To understand it considers a table ‘Employee’ having the following records −
mysql> Select * from employee; +----+--------+--------+ | ID | Name | Salary | +----+--------+--------+ | 1 | Gaurav | 50000 | | 2 | Rahul | 20000 | | 3 | Advik | 25000 | | 4 | Aarav | 65000 | | 5 | Ram | 20000 | | 6 | Mohan | 30000 | | 7 | Aryan | NULL | | 8 | Vinay | NULL | +----+--------+--------+ 8 rows in set (0.00 sec)
Now, the query below will use ORDER BT RAND() to shuffle the set of rows in the result set −
mysql> Select * from Employee ORDER BY RAND(); +----+--------+--------+ | ID | Name | Salary | +----+--------+--------+ | 4 | Aarav | 65000 | | 1 | Gaurav | 50000 | | 3 | Advik | 25000 | | 7 | Aryan | NULL | | 6 | Mohan | 30000 | | 8 | Vinay | NULL | | 5 | Ram | 20000 | | 2 | Rahul | 20000 | +----+--------+--------+ 8 rows in set (0.00 sec)
- Related Articles
- How can we get all the unique rows in MySQL result set?
- How can we get only unique values of a column in MySQL result set?
- How can we get randomly different set of rows or values each time from MySQL table?
- How can we get the summary output of a column in MySQL result set itself?
- How can we divide the result set returned by MySQL into groups?
- How can we handle a result set inside MySQL stored procedure?
- How can we use existing values of the rows to provide new values in the SET clause of UPDATE statement?
- How Can MySQL operator precedence affect result set?
- Can we replace a number with a String in a MySQL result set?
- How can we apply filtering criteria at group levels of the result set returned by MySQL?
- What is the meaning of ‘empty set’ in MySQL result set?
- How is it possible to filter out the duplications in the rows of result set return by MySQL?
- How can we use SET statement to assign a SELECT result to a MySQL user variable?
- How to randomize column values of a data.table object for a set of columns in R?
- How to select an empty result set in MySQL?

Advertisements