SQL Query

SQL Server: SQL Query to Select Top N Rows

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

This guide is specifically for SQL Server syntax.

SQL Query to Select Top N Rows

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

Quick Answer

SELECT * FROM table ORDER BY column DESC TOP 10;

Explanation

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


SQL Server-Specific Notes

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