Getting A Random Row From A PostgreSQL Table

10th January 2008

To get a random row from a PostgreSQL database you need to use the RANDOM() function. This is similar to the MySQL function RAND() and will generate a new random number for each row and order them by that new number. This is used in conjunction with the LIMIT clause to limit the amount of returned rows to one.

SELECT value FROM table ORDER BY RANDOM() LIMIT 1

Add new comment

The content of this field is kept private and will not be shown publicly.
CAPTCHA This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.