Retrieving TOP-N Rows in PostgreSQL
PostgreSQL provides a mechanism for limiting query results using the limit and / or offset SQL syntax. You can use the the following SQL statement to retrieve the top-n rows in a table:
SELECT * FROM employee order by salary desc limit 10;
In the above statement, the query will return the first 10 employees with the highest salary.
The offset feature in PostgreSQL will let you return an arbitrary range of rows (eg return rows 10-20). This is very handy for displaying pages of records:
SELECT * FROM employee order by salary desc limit 10 offset 20;