SQL Query

How to Select Top N Rows in SQL

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

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 TOP 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:

Related Content

From Our Blog

Try it yourself

Practice this query in our interactive SQL sandbox.