SQLite: Select Random Records from a Table

Updated: September 12, 2021 By: A Goodman Post a comment

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.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments