SQL Server Tutorial
In SQL Server, TOP clause is used with SELECT statement to limit the number of rows fetched in the SQL query result.
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.
Let us consider the example 'Employee' database table.
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 ASCOutput:
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 ASCOutput:
SELECT TOP 2 * FROM Employee WHERE Country = 'USA' ORDER BY Country ASCOutput:
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 ASCOutput:
SQL Server Tutorial
Privacy Policy | Copyright2020 - All Rights Reserved. | Contact us | Report website issues in Github | Facebook page | Google+ page