The TOP Clause

TOP

The TOP clause is used to return top N records from the result set. We can also fetch the required percentage rows from the result set.

Syntax:

SELECT TOP Number/Percent <Column Name(s)> FROM Table_Name;

Examples:

TOP with Number:

———————————————————————-
USE [AdventureWorks2012];
GO

SELECT TOP 5 [BusinessEntityID], [JobTitle]
FROM [HumanResources].[Employee];
———————————————————————-

In the below result set, it is fetching only 5 rows out of 290 rows from the [HumanResources].[Employee] table and also displaying 2 columns which we have mentioned in the SELECT statement.

Result set:

TOP

TOP with Percent:

—————————————————————————
USE [AdventureWorks2012];
GO

SELECT TOP 1 PERCENT [BusinessEntityID], [JobTitle]
FROM [HumanResources].[Employee];
—————————————————————————

The above query returns only 3 records as 1% of the records from 290 records is 3.

 Result set:

TOP Percent

Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *