This SQL tutorial focuses on the SQL Server SELECT TOP statement, and provides explanations and examples.
The TOP statement allows limiting the number of records returned from a query, as well as displaying the number of highest/lowest rows, according to a certain condtion (Top N Analysis).
Limiting the Number of Rows
To limit the number of results returned, use the TOP command.
SELECT TOP N column, column, column .. FROM table
For example, the following query returns only the first 10 results.
SELECT TOP 10 first_name FROM employees
SQL Server SELECT TOP Clause – TOP-N Analysis
TOP-N analysis is used to limit the number of rows returned from an ordered set of data items. This are very useful when you would like to return the N lowest or highest rows.
SELECT TOP N column, column, column .. FROM table ORDER BY column
For example, the following SQL Server example returns the top ten earners in the company:
SELECT TOP 10 first_name, salary FROM employees ORDER BY salary DESC
You can use the keyword PERCENT to receive a certain percentage of the data, rather than an arbitrary number of rows. The following example returns the top 10 percent earners in the company.
SELECT TOP 10 PERCENT first_name, salary FROM employees ORDER BY salary DESC