SQL TOP Clause

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, we can use the SQL TOP Clause and extract the required number of rows.

SQL SELECT TOP Clause Syntax

To retrieve the few records or to restrict the number of rows, we have to use the SQL TOP Clause, followed by Expression or Percentage. The Syntax of the SQL Server TOP Clause is

SELECT TOP Expression | Percentage [Column_Names]

FROM [Table_Name]

We can use either the Expression or Percentage along with the SQL TOP clause. 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 use the below shown table data to explain the SQL Select Top Clause.

SELECT [EmpID]
      ,[FirstName]
      ,[LastName]
      ,[Education]
      ,[Occupation]
      ,[YearlyIncome]
      ,[Sales]
  FROM [Customer]
SQL TOP Clause 1

SQL TOP 1 Example

In this SQL Top clause example, We are going to select the first row from the customer’s table.

SELECT TOP 1 [EmpID]
      ,[FirstName]
      ,[LastName]
      ,[Education]
      ,[Occupation]
      ,[YearlyIncome]
      ,[Sales]
  FROM [Customer]

The TOP Clause is retrieving the first row from the table.

SQL TOP Clause 2

SQL TOP 10 Example

Returns the Sql Server Top 10 rows from the customer’s table.

SELECT TOP 10 [EmpID]
      ,[FirstName]
      ,[LastName]
      ,[Education]
      ,[Occupation]
      ,[YearlyIncome]
      ,[Sales]
  FROM [Customer]
SQL TOP Clause 3

SQL TOP * Example

We can also use the * symbol instead of writing all the column names (Not Advisable). Let’s see how the Top * works.

SELECT TOP 5 *
  FROM [Customer]
SQL TOP Clause 4

SQL TOP ORDER BY Statement

In Tables, By default, data will not be inserted in any order. So, If you want to retrieve the first 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 Top Clause to get the first three customers

SELECT TOP 5 [EmpID]
      ,[FirstName]
      ,[LastName]
      ,[Education]
      ,[Occupation]
      ,[YearlyIncome]
      ,[Sales]
  FROM [Customer]
  ORDER BY YearlyIncome DESC

Data will be sorted by the [Yearly Income] in the descending order, then it will retrieve Top five rows.

SQL TOP Clause 5

SQL TOP Where Example

You can also use Where Clause along with the Top clause to restrict the records selected by the select statement. The following Where Clause query selects the first 10 records whose Sales is Greater than 1300. Remember, we also used the ORDER BY to sort the SQL Server data using yearly income in Descending order.

SELECT TOP 10 [EmpID]
      ,[FirstName]
      ,[LastName]
      ,[Education]
      ,[Occupation]
      ,[YearlyIncome]
      ,[Sales]
  FROM [Customer]
  WHERE Sales > 1300
  ORDER BY YearlyIncome DESC
SQL TOP Clause 6

TOP Percentage Example

We use SQL TOP PERCENT to specify the number of customers the query has to return. Here, we are using first 40 PERCENT. It means 6 Rows because there are 15 rows in our customer’s table.

SELECT TOP 40 PERCENT [EmpID]
      ,[FirstName]
      ,[LastName]
      ,[Education]
      ,[Occupation]
      ,[YearlyIncome]
      ,[Sales]
  FROM [Customer]
SQL TOP Clause 7

Let us use the ORDER BY statement along with the TOP Percent.

SELECT TOP 40 PERCENT [EmpID]
      ,[FirstName]
      ,[LastName]
      ,[Education]
      ,[Occupation]
      ,[YearlyIncome]
      ,[Sales]
  FROM [Customer]
  ORDER BY Sales DESC
SQL TOP Clause 8

SQL TOP WITH TIES

In general, the below example will return the first 5 rows of the Customers with the highest yearly income. When we use WITH TIES followed by SQL TOP Clause, 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.

SELECT TOP 5 WITH TIES [EmpID]
      ,[FirstName]
      ,[LastName]
      ,[Education]
      ,[Occupation]
      ,[YearlyIncome]
      ,[Sales]
  FROM [Customer]
  ORDER BY YearlyIncome DESC

Although we Selected the top 5 rows, 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.

SQL TOP Clause 9

NOTE: WITH TIES only work when we use the ORDER BY statement; otherwise, the Server will throw an error.