SQL Server TOP Keyword

SQL Server TOP Keyword

In SQL Server, TOP clause is used with SELECT statement to limit the number of rows fetched in the SQL query result.

TOP Syntax

SELECT [TOP n [PERCENT] column_name1[, column_name2, ....]
FROM table_name
WHERE Filter_condition
[ORDER BY column_name [, column_name]

here n is the number of rows that we need to fetch.

PERCENT is used to denote n in percentage.

Employee Table

Let us consider the example 'Employee' database table.

Employee table output

TOP Example with Row Count

To get the top 2 rows from Employee table in ascending order based on Country column.

SELECT TOP 2 Name, DeptId, City, Country, Email, Salary
FROM Employee
ORDER BY Country ASC
Output:
TOP 2 ORDER BY ASC output

To get the top 1 row from Employee table rows which country column value is 'USA' in ascending order based on Country column.

SELECT TOP 1 Name, DeptId, City, Country, Email, Salary
FROM Employee
WHERE Country = 'USA'
ORDER BY Country ASC
Output:
TOP 1 ORDER BY ASC output

SELECT TOP ROW_COUNT *

SELECT TOP 2 *
FROM Employee
WHERE Country = 'USA'
ORDER BY Country ASC
Output:
TOP 2 * ORDER BY ASC output

TOP Example with Row Count Percentage

To get the top 60 percentage of rows from Employee table in ascending order based on Country column.

SELECT TOP 60 PERCENT Name, DeptId, City, Country, Email, Salary
FROM Employee
ORDER BY Country ASC
Output:
TOP 60 PERCENT ORDER BY ASC output



Python installation

Privacy Policy  |  Copyrightcopyright symbol2020 - All Rights Reserved.  |  Contact us   |  Report website issues in Github   |  Facebook page   |  Google+ page

Email Facebook Google LinkedIn Twitter
^