Top clause is used to limit the number of records in the result set. To limit the number of records, you can specify number of rows or percentage of rows to retrieve in the TOP clause.
SYNTAX I (Top number of rows) :
SELECT TOP [number] Column1, Column2 …
FROM Table1
SYNTAX II (Top percentage of rows) :
SELECT TOP [number] PERCENT Column1, Column2 …
FROM Table1
Table: Employee
EmployeeID FirstName LastName Title HireDate ManagerID
1 George Cruz CEO 12/8/2008 NULL
2 John Mathew Vice President 4/1/2009 1
3 Ethan William Director I 8/8/2009 2
4 Jacob Logan Director II 12/8/2011 3
5 Nathan Mateo Senior Manager 9/3/2009 4
6 Alice Charles Manager I 12/8/2012 5
7 John Robert Manager II 10/8/2011 6
8 Sophia James Manager III 5/8/2011 7
9 Kevin Chan Product Manager 7/8/2011 5
10 Susan Mortiz Solutions Arthitect 1/3/2012 6
11 Martha Miller Systems Arthitect 12/15/2012 10
Example 1 (TOP clause)
Query :
SELECT TOP 5 *
FROM Employee
Output :
EmployeeID FirstName LastName Title HireDate ManagerID
1 George Cruz CEO 12/8/2008 NULL
2 John Mathew Vice President 4/1/2009 1
3 Ethan William Director I 8/8/2009 2
4 Jacob Logan Director II 12/8/2011 3
5 Nathan Mateo Senior Manager 9/3/2009 4
Description :
The above query retrieves top 5 records from the employee table.
Example 2 (TOP clause using percentage)
Query :
SELECT TOP 50 PERCENT *
FROM Employee
Output :
EmployeeID FirstName LastName Title HireDate ManagerID
1 George Cruz CEO 12/8/2008 NULL
2 John Mathew Vice President 4/1/2009 1
3 Ethan William Director I 8/8/2009 2
4 Jacob Logan Director II 12/8/2011 3
5 Nathan Mateo Senior Manager 9/3/2009 4
6 Alice Charles Manager I 12/8/2012 5
Description :
The above query retrieves top 50% records from the employee table.