Sort by RAND in MySQL
Summary: in this tutorial, we will learn how to shuffle or sort the values or records of a table in MySQL.
Most businesses and organizations that use MySQL for data analysis or visualization need to sort different table values of their users according to different criteria.
One of the most effective techniques to test whether different users in MySQL tables are correctly checked is to access users randomly. It can help avoid conflicts and better understand users based on specific platforms.
MySQL uses RAND()
functions to help analysts access random records. Let's try to understand this sentence more deeply.
However, before we begin, let's create a dummy dataset to work with. Here, we create a table student_dates
and a few rows.
-- create the table student_dates
CREATE TABLE student_dates(
stu_id int,
stu_firstName varchar(255) DEFAULT NULL,
stu_date date,
primary key(stu_id)
);
In MySQL, use INSERT
the statement student_dates
to insert entries into the table
The previous query created a student_dates
table called . Now INSERT
with the help of the statement, let's try to add data for some students. This can be done as follows:
-- insert rows to the table student_dates
INSERT INTO student_dates(stu_id,stu_firstName,stu_date)
VALUES(1,"Preet",STR_TO_DATE('24-May-2005', '%d-%M-%Y')),
(2,"Dhruv",STR_TO_DATE('14-June-2001', '%d-%M-%Y')),
(3,"Mathew",STR_TO_DATE('13-December-2020', '%d-%M-%Y')),
(4,"Jeet",STR_TO_DATE('14-May-2003', '%d-%M-%Y')),
(5,"Steyn",STR_TO_DATE('19-July-2002', '%d-%M-%Y'));
The above code will student_dates
enter the student data in the table.
ORDER BY
Use the statement in MySQL student_dates
to randomly sort the values of the table
As mentioned above, we can sort the values using the statement in MySQL order by
. This logic can also be used to randomly sort the records in a table. It can be done using the following method.
SELECT * from name_of_the_table
ORDER BY RAND();
As we can see above, all the table records will be RAND()
randomly sorted using the %s function. We can now apply this concept to our % student_dates
s table. This can be done using the following query.
SELECT * from student_dates
ORDER BY RAND();
The output of the above code can be explained as follows.
stu_id stu_firstName stu_date
5 Steyn 2002-07-19
3 Mathew 2020-12-13
4 Jeet 2003-05-14
1 Preet 2005-05-24
2 Dhruv 2001-06-14
As we can see in the above code block, all student_dates
the table records are sorted randomly. If the above query is executed a few more times, the output will be as shown below.
stu_id stu_firstName stu_date
4 Jeet 2003-05-14
1 Preet 2005-05-24
2 Dhruv 2001-06-14
3 Mathew 2020-12-13
5 Steyn 2002-07-19
As we can see from the above output block, the values are randomly ordered.
Therefore, with the help of ORDER BY
statement and RAND()
function, we can effectively randomize the different records of a particular table in MySQL.
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
If ELSE in MySQL
Publish Date:2025/04/11 Views:85 Category:MySQL
-
In this tutorial, we aim to explore how to use IF ELSE the statement in MySQL. One of the key roles of a data analyst is to gather insights from the data and produce meaningful results. It can be done with the help of several data filtering
DATETIME vs. TIMESTAMP in MySQL
Publish Date:2025/04/11 Views:117 Category:MySQL
-
DATETIME and TIMESTAMP are two different data types that can be used to store values that must contain both a date and a time portion. In this article, we will understand how it is stored in the database and the memory required for ea
Execute multiple joins in one query in MYSQL
Publish Date:2025/04/11 Views:94 Category:MySQL
-
Have you ever wondered how to include multiple joins in one query in MySQL? You have come to the right place. Remember that joins allow us to access information from other tables. This information is included separately to avoid redundancy.
Joining 3 tables in MySQL
Publish Date:2025/04/11 Views:187 Category:MySQL
-
In this tutorial, we will learn how to join three tables in MySQL. Businesses and organizations may have to visualize three tables simultaneously based on certain matching columns common to all three tables. This operation is allowed in MyS
Use of UPDATE JOIN in MySQL
Publish Date:2025/04/11 Views:85 Category:MySQL
-
This tutorial will explain how to use the statement in MySQL database UPDATE JOIN . We generally use joins to iterate over the rows in a particular table which may or may not have similar rows in other tables. We can UPDATE use JOIN the cla
How to use the Row_Number() function in MySQL
Publish Date:2025/04/11 Views:142 Category:MySQL
-
In this tutorial, we will explain how to use the VALUES function in MySQL ROW_NUMBER() . This is a sorting method that assigns consecutive numbers within a partition starting from 1. It is important to note that no two rows within a partiti
Multiple primary keys in MySQL
Publish Date:2025/04/11 Views:66 Category:MySQL
-
In this tutorial, our goal is to explore the concept of multiple primary keys for a table in MySQL. Many times, businesses and organizations have to assign certain columns as primary keys. This primary key has multiple uses and reasons to b
Displaying foreign keys in MySQL
Publish Date:2025/04/11 Views:55 Category:MySQL
-
In this tutorial, we aim to explore how to display foreign keys for tables and columns in MySQL. The type of key that references a primary key, also known as the primary key of another table, is called a foreign key. Understanding the forei
Select first N rows in MySQL
Publish Date:2025/04/11 Views:85 Category:MySQL
-
Sometimes, you have to select first N rows of MySQL database according to your project requirements. n The value of varies according to the requirement; it can be TOP 1 row or TOP 30 rows. We will learn how to select top N rows using the cl