The SQL TOP clause is used to limit the number of rows that are returned by a query. Usually, Databases, tables hold millions or billions of records. If you want to see the top 10 products, there is no point in retrieving all the 1 million records. We can use the SQL TOP Clause and extract the required number of rows.
For example, If you are a Sales Manager and your job is to identify the top 10 highest performing products (Products with the highest Sales). You can use SQL Top Clause. For this, you can write the query as Select Top 10 Product Name from Products Table Order By Sales in Descending Order. By seeing the result, you can understand that x is performing well. Using this data, you can request the management to increase the production of those ten products.
SQL SELECT TOP Clause Syntax
To retrieve the few records or to restrict the number of rows, we have to use the TOP Clause, followed by Expression or Percentage.
The Syntax of the SQL Server TOP Clause is
-- SQL TOP Clause Syntax SELECT TOP Expression | Percentage [Column_Names] FROM [Table_Name]
From the above syntax, you can see that we can use either the Expression or Percentage along with the TOP clause in SQL Server.
For instance, TOP 1 means it will retrieve the first record. If we write TOP 10 Percent, then the SELECT Statement will extract 10% records from total records. You can also use the ORDER BY statement to sort the data accordingly.
We are going to use the below-shown data to explain the Select Top Clause in SQL Server with an example.
-- SQL Server Top Clause Example USE [SQL Tutorial] GO SELECT [EmpID] ,[FirstName] ,[LastName] ,[Education] ,[Occupation] ,[YearlyIncome] ,[Sales] FROM [Customer]
OUTPUT
SQL TOP 1 Example
In this SQL Top clause example, We are going to select the Top 1 row from the customer’s table.
-- SQL Server Top 1 Example SELECT TOP 1 [EmpID] ,[FirstName] ,[LastName] ,[Education] ,[Occupation] ,[YearlyIncome] ,[Sales] FROM [Customer]
OUTPUT
From the above screenshot, you can observe that the TOP Clause is retrieving the first record from the table.
SQL TOP 10 Example
This example returns the Sql Server Top 10 rows from the customer’s table.
-- SQL Server Top 10 Example SELECT TOP 10 [EmpID] ,[FirstName] ,[LastName] ,[Education] ,[Occupation] ,[YearlyIncome] ,[Sales] FROM [Customer]
OUTPUT
SQL TOP * Example
We can also use the * symbol instead of writing all the column names (Not Advisable). Let’s see how the SQL Top * works.
-- SQL Server Top * Example SELECT TOP 5 * FROM [Customer]
OUTPUT
SQL TOP ORDER BY Statement
In Tables, By default, data will not be inserted in any order. So, If you want to retrieve the Top three customers who are having the highest yearly income, then you have to sort the data using ORDER BY statement. Next, you have to use this SQL Top Clause to get the top three customers
-- SQL Server Top Order By Example SELECT TOP 5 [EmpID] ,[FirstName] ,[LastName] ,[Education] ,[Occupation] ,[YearlyIncome] ,[Sales] FROM [Customer] ORDER BY YearlyIncome DESC
Now the data will be sorted by the [Yearly Income] in the descending order, then it will retrieve Top five records.
OUTPUT
SQL TOP Where Example
You can also use Where Clause along with the Sql Server Top clause to restrict the records selected by the select statement.
The following query selects the Top 10 records whose Sales is Greater than 1300. Remember, we also used the ORDER BY to sort the data using yearly income in Descending order.
-- SQL Server Top Clause Example SELECT TOP 10 [EmpID] ,[FirstName] ,[LastName] ,[Education] ,[Occupation] ,[YearlyIncome] ,[Sales] FROM [Customer] WHERE Sales > 1300 ORDER BY YearlyIncome DESC
OUTPUT
SQL TOP Percentage Example
In this example, we are going to use SQL TOP PERCENT to specify the number of customers the query has to return. In the below Select statement, we are using SQL TOP 40 PERCENT. It means 6 Rows because there are 15 records in our customer’s table.
-- SQL Server Top Clause Example SELECT TOP 40 PERCENT [EmpID] ,[FirstName] ,[LastName] ,[Education] ,[Occupation] ,[YearlyIncome] ,[Sales] FROM [Customer]
OUTPUT
Let us use the ORDER BY statement along with the TOP Percent in Sql Server.
-- SQL Server Top Clause Example SELECT TOP 40 PERCENT [EmpID] ,[FirstName] ,[LastName] ,[Education] ,[Occupation] ,[YearlyIncome] ,[Sales] FROM [Customer] ORDER BY Sales DESC
OUTPUT
SQL TOP WITH TIES
In general, the below example will return the top 5 records of the Customers with the highest yearly income. When we use WITH TIES followed by TOP Clause in SQL Server, then it will return the top 5 records. Plus, all the records whose yearly income is equal to the last record (5th) of the select statement.
-- SQL Server Top Clause Example SELECT TOP 5 WITH TIES [EmpID] ,[FirstName] ,[LastName] ,[Education] ,[Occupation] ,[YearlyIncome] ,[Sales] FROM [Customer] ORDER BY YearlyIncome DESC
OUTPUT
From the above screenshot, you can observe that, Although we selected the top 5 records, the output is displaying 7 records. Because the yearly income of 5, 6, and 7 records are the same. If these are some more records with [YearlyIncome] = 80000, then they will display in the result window.
NOTE: WITH TIES only work when we use the ORDER BY statement otherwise it will throw an error