Get Top 10 rows in postgresql (TOP N rows and First N rows)

Returning only the first N records in postgresql can be accomplished using limit keyword. Let’s see how to get top 10 rows in postgresql  and Get First N rows in postgresql.

Let’s depict with an Example. The table we use for depiction is

ExamScore:

Get First N rows in postgresql 1

Get First N rows in Postgresql:

SELECT * FROM ExamScore LIMIT 5;

Gets first 5 rows from the table as shown below

Get First N rows in postgresql 2

 

Get Top N rows in Postgresql:

Let’s get Top 5 Rows based on Sciencescore

SELECT * FROM ExamScore order by Sciencescore desc LIMIT 5;

So the top 5 rows based on Sciencescore will be,

Get First N rows in 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.