TOP in SQL
The SQL SELECT TOP command retrieves records from one or more tables in a database and allows us to limit the query result set based on the number of rows or the percentage of rows. This clause is used when the database tables contain thousands of records. In most cases, the TOP and ORDER BY constructions are used in conjunction. Otherwise, the TOP clause will return the first N rows in an arbitrary order. As a result, it is best practice to utilize the TOP clause in conjunction with an ORDER BY to produce a specific sorted result.
Consider the following example for a better understanding of TOP in SQL:
If a Student table contains a large quantity of data about students, the select TOP statement decides how much students' data will be fetched from the provided database. In this tutorial, we will learn how to use SQL SELECT TOP queries and strengthen our understanding with examples.
NOTE:
The TOP clause is not supported by all databases. For example, Oracle employs the ROWNUM command to fetch a limited amount of entries, while MySQL allows the LIMIT clause to fetch a limited number of records.
How Does TOP Work In SQL?
TOP in SQL (MySql LIMIT, Oracle ROWNUM) is a very useful command for selecting and filtering rows in large tables with thousands of records. This TOP keyword comes in use when we do not want to retrieve all of the rows that meet the requirement, as if there are millions of entries and you execute only the SELECT command, the query will take a long time and close your Query window. This keyword is also used to retrieve the top (n-highest) and lowest (n-lowest) data from a table.
When you use TOP in conjunction with INSERT, UPDATE, MERGE, or DELETE, the referenced rows are not sorted in any particular order. Also, you cannot explicitly define the ORDER BY clause in these statements. If you need to use TOP to insert, delete, or alter rows in relevant chronological order. In that case, it is recommended to use TOP with an ORDER BY clause specified in a subselect statement.
Syntax
Keyword | Description |
---|---|
expression | The TOP keyword is followed by an expression that specifies the number of rows to be returned. If PERCENT is used, the expression is evaluated to a float value; otherwise, it is transformed to a BIGINT value. |
PERCENT | The PERCENT keyword specifies that the query returns the first N percentage of rows, where N is the expression result. For example, if you use TOP \* 50 PERCENT, then it will return the top half of the table. |
WITH TIES | WITH TIES enables you to produce more rows with values matching the last row in the limited result set. It is important to note that WITH TIES may result in more rows being returned than you specified in the expression. For example, if you wish to return the most expensive products, use TOP 1. However, there is a possibility that two or more products will have the same price as the most costly product, and then the other most costly products in the result set might get missed. You can avoid this by using TOP 1 WITH TIES. It will cover not just the first and most costly products but also the second and subsequent ones, and so on. |
Examples For TOP In SQL
The five SQL examples below will show you how to use the TOP in SQL command with PERCENT and WITH TIES keyword in the SQL :
Using the TOP keyword
Example - 1 : In this example, we have three columns in a Cars table, namely Car_Name, Car_Color, and Car_Cost :
Car_Name | Car_Color | Car_Cost |
---|---|---|
Hyundai Creta | White | 10,85,000 |
Hyundai Verna | Black | 9,50,000 |
Toyota Fortuner | Black | 46,50,000 |
Swift Dezire | Silver | 9,80,000 |
Kia Seltos | Blue | 8,00,000 |
Mahindra XUV 500 | White | 20,00,000 |
Assume you wish to display the first three-car names and colours from the table above. To accomplish this, enter the following TOP in the SQL query:
In the SQL output, this query displays the following table on the screen :
Car_Name | Car_Color |
---|---|
Hyundai Creta | White |
Hyundai Verna | Black |
Toyota Fortuner | Black |
Example - 2 : In this example, we have three columns in a Student table, namely Student_ID, Student_Name, and Student_Marks :
Student_ID | Student_Name | Student_Marks |
---|---|---|
9589 | Anand | 87 |
9592 | Ankit | 67 |
9594 | Dushyant | 45 |
9597 | Vishal | 70 |
9600 | Smita | 90 |
Assume you want to display the details of the first four students from the above table. To accomplish this, enter the following TOP in the SQL query :
In the SQL output, this query displays the following table on the screen :
Student_ID | Student_Name | Student_Marks |
---|---|---|
9589 | Anand | 87 |
9592 | Ankit | 67 |
9594 | Dushyant | 45 |
9597 | Vishal | 70 |
Example - 3: In this example, we have five columns in a Customer table, namely ID, NAME, AGE, ADDRESS, and SALARY :
ID | NAME | AGE | ADDRESS | SALARY |
---|---|---|---|---|
1 | Hardik | 32 | Ahmedabad | 20,000.00 |
2 | Komal | 23 | Delhi | 15,000.00 |
3 | Nishi | 27 | Delhi | 65,000.00 |
4 | Aman | 22 | Bhopal | 85,000.00 |
5 | Jai | 24 | Indore | 1,00,000.00 |
Assume you want to display the details of the first three customers from the above table. To accomplish this, enter the following TOP in the SQL query :
In the SQL output, this query displays the following table on the screen :
ID | NAME | AGE | ADDRESS | SALARY |
---|---|---|---|---|
1 | Hardik | 32 | Ahmedabad | 20,000.00 |
2 | Komal | 23 | Delhi | 15,000.00 |
3 | Nishi | 27 | Delhi | 65,000.00 |
Using the TOP PERCENT keyword
Example - 4: In this example, we have three columns in a Bikes table, namely Bike_Name, Bike_Color, and Bike_Cost :
Bike_Name | Bike_Color | Bike_Cost |
---|---|---|
Kawasaki Ninja | Green | 5,00,000 |
Triumph Trident 660 | Orange | 7,58,000 |
Ducati Monster | Black | 11,42,000 |
Harley-Davidson Iron 883 | Black | 10,14,000 |
KTM 390 Duke | Orange | 2,87,000 |
BMW Tiger | Blue | NULL |
Assume you want to display 50 percent data from the above table. To accomplish this, enter the following TOP in the SQL query :
In the SQL output, this query displays the following table on the screen :
Bike_Name | Bike_Color | Bike_Cost |
---|---|---|
Kawasaki Ninja | Green | 5,00,000 |
Triumph Trident 660 | Orange | 7,58,000 |
Ducati Monster | Black | 11,42,000 |
Using the TOP WITH TIES keyword
Example - 5 : In this example, we have four columns in a Beverages table, namely Product_Name, Product_Volume, Product_Price, and Product_Quantity :
Product_Name | Product_Volume | Product_Price | Product_Quantity |
---|---|---|---|
Red Bull | 250 ml | 100.00 | 1 |
Ocean | 250 ml | 71.00 | 2 |
Fanta | 250 ml | 29.00 | 3 |
Pepsi | 250 ml | 29.00 | 2 |
Mirinda | 250 ml | 29.00 | 2 |
Coca Cola | 250 ml | 29.00 | 2 |
In the SQL output, this query returns the top three most expensive products :
Product_Name | Product_Price |
---|---|
Red Bull | 100.00 |
Ocean | 71.00 |
Fanta | 29.00 |
Pepsi | 29.00 |
Mirinda | 29.00 |
Coca Cola | 29.00 |
The third most costly product has a list price of . Since the TOP in SQL command is used WITH TIES, it returned three more products with the same list price as the third.
Learn more
Conclusion
- The TOP in SQL is used to get records from one or more tables in a database and limit the number of records returned based on a set value or percentage. This command comes in handy when working with huge databases.
- The TOP in SQL Server statement restricts the number of rows returned in a query result set to a specified number or percentage of rows.
- When you use the TOP in SQL with the ORDER BY clause, the result set is limited to the first N sorted rows. Otherwise, TOP returns the first N rows in an unspecified order.