|
SQL > Advanced SQL >
Top
The TOP keyword restricts the number of results returned from a SQL statement in Microsoft SQL Server.
SQL TOP is SQL Server-specific and limits returned rows to either a fixed count (TOP N) or a percentage of results (TOP N PERCENT) — combine with ORDER BY to ensure meaningful results.
Syntax
The syntax for TOP is as follows:
SELECT TOP [TOP argument] "column_name"
FROM "table_name";
where [TOP argument] can be one of two possible types:
1. [N]: The first N records are returned.
2. [M] PERCENT: The number of records corresponding to M% of all qualifying records are returned.
Examples
We use the following table for our examples.
Table Store_Information
| Los Angeles | 1500 | Jan-05-1999 |
| San Diego | 250 | Jan-07-1999 |
| San Francisco | 300 | Jan-08-1999 |
| Boston | 700 | Jan-08-1999 |
Example 1: [TOP argument] is an integer
To show the two highest sales amounts in Table Store_Information, we key in,
SELECT TOP 2 Store_Name, Sales, Txn_Date
FROM Store_Information
ORDER BY Sales DESC;
Result:
| Store_Name | Sales | Txn_Date |
| Los Angeles | 1500 | Jan-05-1999 |
| Boston | 700 | Jan-08-1999 |
Example 2: [TOP argument] is a percentage
To show the top 25% of sales amounts from Table Store_Information, we key in,
SELECT TOP 25 PERCENT Store_Name, Sales, Txn_Date
FROM Store_Information
ORDER BY Sales DESC;
Result:
| Store_Name | Sales | Txn_Date |
| Los Angeles | 1500 | Jan-05-1999 |
Frequently Asked Questions
- What does SQL TOP do?
- SQL TOP is a SQL Server keyword that limits the rows returned by a SELECT statement. TOP 2 returns the first 2 rows; TOP 25 PERCENT returns the top 25% of qualifying rows. Always pair it with ORDER BY for predictable results.
- What is the MySQL or Oracle equivalent of TOP?
- MySQL uses LIMIT:
SELECT * FROM table ORDER BY Sales DESC LIMIT 5; Oracle uses ROWNUM or FETCH FIRST: SELECT * FROM table FETCH FIRST 5 ROWS ONLY; These are not interchangeable — each is database-specific.
- Can you use TOP without ORDER BY?
- Technically yes, but without ORDER BY the database can return any N rows — there is no guarantee which rows you get. Always use ORDER BY with TOP to retrieve the top or bottom records by a meaningful column.
- How does TOP N PERCENT work in SQL Server?
- TOP N PERCENT returns the number of rows equal to N% of the total qualifying rows. With 4 rows and TOP 25 PERCENT, 1 row is returned (25% of 4). SQL Server rounds up fractional results to the next whole row.
Next: SQL Subquery
This page was last updated on March 19, 2026.
Copyright © 2026 1keydata.com All Rights Reserved
Privacy Policy About Contact |