Select random records using ORDER BY RAND() in MySQL - MySQL Developer Tutorial

Select random records using ORDER BY RAND() in MySQL 

Scenario : 

Let's say you are working as MySQL Developer and you are asked to provide some sample data from a table. You need to provide 10 rows or 100 rows but they should be selected random from table.

Solution : 

You can use Rand ( ) function with Limit to get RANDOM ROWS from table. By using Limit you will restrict the number of rows you want to return.

Select * from YourTableName Order by RAND() Limit Count;


Let's say if we want to get 10 Random rows from customer table, we can use below statement in MySQL or MairaDB.


Select * from customer order by RAND () Limit 10;





No comments:

Post a Comment

Note: Only a member of this blog may post a comment.