SQL Tutorial – Randomly select data from a SQL Table.

Suppose you have a customer table and you want to randomly select few rows of data from it.

Solution –

To solve this problem first you have to use any built-in function supported by your database that randomly return values. Then use this function in the ORDER BY clause to randomly order the data and then use the LIMIT to limit to the number of rows of data that you want. In MySQL it is RAND() function and in PostgreSQL it is RANDOM()

SELECT *
FROM customer
ORDER BY Rand() 
LIMIT 5;

Rating: 1 out of 5.

Leave a Reply