Fetch random rows from database table in MySQL
Tagged:

If you need to develop applications (say for online tests) where you want the questions to appear on a random order every time a new test is taken, this MySQL query may prove helpful.


SELECT * FROM yourtablename ORDER BY RAND() LIMIT 10

In this query, the function RAND() returns a random floating-point value in the range between 0 and 1 for every row that it fetches and sorts them in order of this value. Since this value is generated on a random basis every time this query is run, the order that the row is fetched varies. Then, we limit the rows that we have fetched from our MySQL query as required with LIMIT in the query (I have limited it to 10 questions).