Selecting a random row from a SQLite table

Posted: 12/03/2014

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;