Select random rows from Postgresql

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.

Select random rows from Postgresql 1

 

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

Select random rows from Postgresql 2

 

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

Select random rows from Postgresql 3

 

                                                                                                         

Author

  • Sridhar Venkatachalam

    With close to 10 years on Experience in data science and machine learning Have extensively worked on programming languages like R, Python (Pandas), SAS, Pyspark.

    https://www.linkedin.com/in/sridhar-venkatachalam-74414252/ karaimadai@sg2plcpnl0204.prod.sin2.secureserver.net Venkatachalam Sridhar