T-SQL Tutorial

SQL TOP


The TOP clause in SQL Server is used to specify the number of rows to be returned in a SELECT statement. It can be used in conjunction with the ORDER BY clause to return the top N records based on a specific sort order.


TOP syntax

SELECT TOP number column_name(s) FROM table_name
SELECT TOP percent column_name(s) FROM table_name


TOP examples

For example, the following query would return the top 10 records from the "Employees" table, sorted by last name in ascending order:

SELECT TOP 10 *
FROM Employees
ORDER BY LastName ASC;


You can also use the TOP clause with a percentage value, in which case it will return the specified percentage of rows. For example, the following query would return the top 20% of records from the "Employees" table, sorted by hire date in descending order:

SELECT TOP 20 PERCENT *
FROM Employees
ORDER BY HireDate DESC;


It's important to note that the TOP clause is only available in the SELECT statement and it can't be used in an INSERT, UPDATE or DELETE statement. Also, the TOP clause is not a standard SQL statement. It's specific to SQL Server.