SQL Query

MySQL: SQL Query to Select Top N Rows

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

This guide is specifically for MySQL syntax.

SQL Query to Select Top N Rows

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


MySQL-Specific Notes

This page covers MySQL 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.