karthi_ms
karthi_ms

Reputation: 5698

Selecting random rows

Hi all, My requirement is simple.I want to select random rows from a table.

For example my table having 10 rows I want to select any three rows randomly.Is there any way in psql.

Upvotes: 2

Views: 2765

Answers (3)

Martin S.
Martin S.

Reputation: 546

Please be aware that once your table grows the "order by random/limit" approach will be slow, since it requires a whole table scan.

See http://blog.rhodiumtoad.org.uk/2009/03/08/selecting-random-rows-from-a-table/ for an alternative solution.

Upvotes: 4

Vivek Goel
Vivek Goel

Reputation: 24140

Use the random function.

SELECT * FROM tablename ORDER BY random() LIMIT 3;

Upvotes: 10

Abubacker Nainamohamed
Abubacker Nainamohamed

Reputation: 4797

Try this !

$ select * from table_name order by random() limit 3 ;

Upvotes: 1

Related Questions