Select TOP
The SELECT TOP statement retrieves records from one or more tables or views and limits the number of records returned based on a fixed value or percentage.
SQL Select TOP syntax:
SELECT TOP number/percentage
Column1
FROM Table_Name
WHERE condition;
Example:
Lets select the TOP 4 rows from the following table.
| SalesReasonID |
Name |
ReasonType |
|---|---|---|
| 1 |
Price |
Other |
| 2 |
On Promotion |
Promotion |
| 3 |
Magazine Advertisement |
Marketing |
| 4 |
Television Advertisement |
Marketing |
| 5 |
Manufacturer |
Other |
| 6 |
Review |
Other |
| 7 |
Demo Event |
Marketing |
| 8 |
Sponsorship |
Marketing |
| 9 |
Quality |
Other |
| 10 |
Other |
Other |
SELECT TOP 4
SalesReasonID ,
Name ,
ReasonType
FROM SalesReason
Results:
| SalesReasonID |
Name |
ReasonType |
|---|---|---|
| 1 |
Price |
Other |
| 2 |
On Promotion |
Promotion |
| 3 |
Magazine Advertisement |
Marketing |
| 4 |
Television Advertisement |
Marketing |
Lets select TOP 50 percent of data.
SELECT TOP 50 percent
SalesReasonID ,
Name ,
ReasonType
FROM SalesReason
Results:
| SalesReasonID |
Name |
ReasonType |
|---|---|---|
| 1 |
Price |
Other |
| 2 |
On Promotion |
Promotion |
| 3 |
Magazine Advertisement |
Marketing |
| 4 |
Television Advertisement |
Marketing |
| 5 |
Manufacturer |
Other |
Use a where condition to further filter down withing the result set.
SELECT TOP 50 percent
SalesReasonID ,
Name ,
ReasonType
FROM SalesReason
WHERE ReasonType = ‘Marketing’
Results:
| SalesReasonID | Name | ReasonType |
|---|---|---|
| 3 | Magazine Advertisement | Marketing |
| 4 | Television Advertisement | Marketing |
SQL Tutorial