Selecting a random row from a SQLite table


Scenario:

You want are wanting to select a random row from a SQLite table which may not have a full sequential ID list (because records may have been inserted/deleted leaving gaps in the ID list or there may not be an id list).

Solution:

Luckly, the RANDOM() function makes this very simple. You can use the RANDOM() function in order to get a random record and then limit the result set to only 1 record.

SQL:

SELECT * FROM your_table ORDER BY RANDOM() LIMIT 1;

Leave a comment

Please note that we won't show your email to others, or use it for sending unwanted emails. We will only use it to render your Gravatar image and to validate you as a real person.