In order to Select the random rows from postgresql we use RANDOM() function. RANDOM() Function in postgresql generate random numbers . Let’s see how to
- Get the random rows from postgresql using RANDOM() function.
- Get Random percentage of rows from a table in postresql.
We will be using Student_detail table.
Get Random N rows from postgresql:
SELECT * FROM student_detail ORDER BY RANDOM() LIMIT 4;
We will be generating 4 random rows from student_detail table. So the resultant table will be
Get Random rows from postgresql (random percent of rows):
select * from student_detail where random() < 0.7
We will be generating random numbers between 0 and 1, then will be selecting with rows less than 0.7. So the resultant table will be with random 70 % rows