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:
SELECT * FROM ExamScore LIMIT 5;
Gets first 5 rows from the table as shown below
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,