SQL Query

PostgreSQL: How to Select Top N Rows in SQL

PostgreSQL guide: Retrieve the first N rows from a result set using LIMIT, TOP, or FETCH.

This guide is specifically for PostgreSQL syntax.

How to Select Top N Rows in SQL

Retrieve the first N rows from a result set using LIMIT, TOP, or FETCH.

Quick Answer

SELECT * FROM table ORDER BY column DESC LIMIT 10;

Explanation

LIMIT is used in MySQL/PostgreSQL, TOP in SQL Server. Always use ORDER BY to get consistent results. OFFSET skips rows for pagination.

Query Variants

Mysql

SELECT * FROM employees ORDER BY salary DESC LIMIT 10;

Sqlserver

SELECT LIMIT 10 * FROM employees ORDER BY salary DESC;

Postgres

SELECT * FROM employees ORDER BY salary DESC FETCH FIRST 10 ROWS ONLY;

With Offset

SELECT * FROM employees ORDER BY salary DESC LIMIT 10 OFFSET 20;

Pro Tips

  • Always ORDER BY for predictable results
  • Use OFFSET for pagination
  • Different databases have different syntax

Related SQL Queries

Continue learning with more SQL query examples:


PostgreSQL-Specific Notes

This page covers PostgreSQL syntax. Other databases may have different syntax for similar operations.

Related Content

From Our Blog

Try it yourself

Practice this query in our interactive SQL sandbox.