;

How to Get Random Records from Database Table in MySQL


Tutorialsrack 29/10/2023 SQL MySQL

In this article, we will learn about how to get random records from the database table in MySQL. 

In MySQL, there is no built-in function to retrieve the random records. In order to achieve this, you can use the ORDER BY RAND() clause in your SQL query. 

Here's an example of how to do this:

Syntax
SELECT * FROM YourTableName 
ORDER BY RAND() 
LIMIT N;

Where,

  • N = Replace N with the number of random records you want to retrieve,
  • YourTableName = Replace YourTableName with the name of the table from which you want to fetch random records,
  • ORDER BY RAND() = This is the main part of the query. It orders the result set by a random value generated by the RAND() function. This effectively shuffles the records in a random order.

Example: Get Random Records from MySQL

Example: Get Random Records from MySQL
SELECT * FROM EMPLOYEES
ORDER BY RAND()
LIMIT 5;

I hope this article will help you to understand how to get random records from the database table in MySQL. 

Share your valuable feedback, please post your comment at the bottom of this article. Thank you!


Related Posts



Comments

Recent Posts
Tags