Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PostgreSQL select 20 random records [duplicate]

Tags:

sql

postgresql

How would I select 20 random rows in a SQL (PostgreSQL) query?

like image 817
PeeHaa Avatar asked May 22 '11 21:05

PeeHaa


People also ask

How do I select a random row in PostgreSQL?

There's an easy way to show a random record in a table: SELECT * FROM table_name ORDER BY RANDOM() LIMIT 1; But this query might take a while to finish as it reads the whole table first then take out a random record from it.

How do I generate random data in PostgreSQL?

The random() Function A function that generates a random float from 0 to 1. Example: INSERT INTO test (id) VALUES (trunc(random()*100)); This will generate an id of three digits from 0 to 100 (trunc to make the float an integer).

What is fetch in PostgreSQL?

FETCH retrieves rows using a previously-created cursor. A cursor has an associated position, which is used by FETCH . The cursor position can be before the first row of the query result, on any particular row of the result, or after the last row of the result. When created, a cursor is positioned before the first row.


1 Answers

SELECT column FROM table
ORDER BY RANDOM()
LIMIT 20
like image 187
ditkin Avatar answered Oct 15 '22 13:10

ditkin