SQL tutorial
SQL Select Top
Learn more about SQL, a standard language for interacting with databases and storing, manipulating, and retrieving data from databases.
Go hands-on with SQL in our free interactive SQL tutorial.
Using the SQL TOP
keyword in a SELECT
statement is equivalent to the LIMIT
clause and differs only slightly – in its availability and functionality. Both are used to truncate the results of a query and return only a specified number of rows from its execution.
There is no SELECT TOP
statement in some SQL databases, for example, both MySQL and PostgreSQL do not have it but can equivalently use LIMIT
instead. Conversely, Microsoft SQL Server has the TOP
keyword which is used instead of a LIMIT
clause. Some other database types, for example Amazon Redshift, have both.
For a SQL database type, a query using a SELECT TOP
statement has a nearly equivalent syntax to modifying query behavior with LIMIT
. The two queries below would return the same result, one using TOP
and the other LIMIT
:
SELECT * FROM employees LIMIT 10;
SELECT TOP 10 * FROM employees;
One major difference of note is that TOP
can be used together with the PERCENT
keyword in order to retrieve a percentage of rows in a table, as opposed to specifying a number of rows.
We could retrieve the top 10 percent of data from the employees
table using the below, even if we do not know the size of the table:
SELECT TOP 10 PERCENT * FROM employees;
Performing the equivalent in a SQL distribution without the TOP
and PERCENT
keywords would require calculating the number of rows in the table.
Learn SQL Today
Get hands-on experience writing code with interactive tutorials in our free online learning platform.
- Free and fun
- Designed for beginners
- No downloads or setup required