SQL Select Top
The SELECT TOP feature lets you limit how many rows a query returns, which is useful when a table holds far more data than you want to see at once.
Why limit the number of rows?
Tables in real applications can contain thousands or millions of rows. When you only need a small sample, a quick preview, or the first few records after sorting, returning every row is wasteful. Row-limiting clauses tell the database to stop after a certain number of results, which keeps queries fast and output readable.
There is no single standard keyword for this across every database. The wording depends on which database engine you are using, so it helps to know the two most common styles: TOP and LIMIT.
The dialect differences
- SQL Server and MS Access use SELECT TOP, written right after the SELECT keyword.
- MySQL, PostgreSQL, and SQLite use LIMIT, written at the end of the query.
- Oracle 12c and later use FETCH FIRST n ROWS ONLY, which is the closest to the official SQL standard.
- TOP can also take a percentage in SQL Server by writing TOP 50 PERCENT.
SQL Server / MS Access syntax (TOP)
SELECT TOP 3 first_name, last_name, salary
FROM Employees;MySQL / PostgreSQL / SQLite syntax (LIMIT)
SELECT first_name, last_name, salary
FROM Employees
LIMIT 3;Combining with ORDER BY
The row limit is applied after sorting, so pairing it with ORDER BY is how you answer questions like 'who are the three highest-paid employees?' The example below sorts by salary from highest to lowest and then keeps only the first three rows.
Highest paid employees
SELECT first_name, last_name, salary
FROM Employees
ORDER BY salary DESC
LIMIT 3;Exercise: SQL Select Top
What is the purpose of SELECT TOP?