Kinda Code
Home/Mobile/SQLite: Select Random Records from a Table

SQLite: Select Random Records from a Table

Last updated: September 12, 2021

In SQLite, you can randomly get one or multiple rows from a table by using the following select statement:

SELECT column1, column2, ...
FROM <table name>
ORDER BY RANDOM()
LIMIT <number of rows you want>;

Example

Let’s say we have a table named users:

ColumnData Type
idINTEGER
emailTEXT
nameTEXT
passwordTEXT

If we want to retrieve 10 (you can get less than 10 results if the table doesn’t have enough data) users at random:

SELECT *
FROM users
ORDER BY RANDOM()
LIMIT 10;

See also: Flutter & SQLite: CRUD Example.

Happy coding.