Introduction to TOP in SQL
TOP is a keyword in SQL SERVER. It is used along with the SELECT clause to limit the number of records to be returned. It comes handy during data analysis. Especially when we just want to see only the first few records to understand the dataset or when we want to find the highest or lowest records from an ordered set of records.
Moving ahead, let’s discuss the SQL TOP keyword in greater detail. The standard syntax for writing it along with the SELECT clause is as follows:
SELECT TOP (NUMBER) | (PERCENT)
[WHERE conditions] [ORDER BY expression [ ASC | DESC ]];
The different parameters used in the syntax are:
- TOP (NUMBER): Returns the top number of rows from the resulting recordset. For example, TOP 5 would return the top 5 rows from the resulting set.
- TOP PERCENT: Returns the top number of rows from the resulting recordset. For example, TOP 50 PERCENT would return the top 50% rows from the resulting set.
- Expressions: Mention the aggregate functions or other functions like Distinct etc. For example, SELECT TOP 1 SUM(column_name) would return the first row from the resulting set after performing the sum on column_name.
- Column_name: Mention the column names, which have to be returned in the resultant recordset.
- FROM tables: Mention the table names, from which the records have to be fetched.
- WHERE conditions: It is used to filter records. WHERE condition will return only those records that fulfill the mentioned condition.
- ORDER BY expression [ ASC | DESC ]: This keyword is used to sort the resulting record set in ascending or descending order. If you do not mention anything from ASC|DESC then ORDER BY will sort the records in ascending order by default.
From the above-mentioned parameters, TOP NUMBER OR PERCENT, Column_names and FROM tables are compulsory. Other keywords/parameters can be used based on the requirement. We can also use other SQL keywords, such as JOIN, having, etc. in the given syntax.
Examples of TOP in SQL
In order to demonstrate and explain the TOP keyword effectively, we will be using the following table. It is a sample “customers” table that contains 15 records with each customer’s id, name, city and the country he/she belongs to.
Schema of “customers” table:
Number of records: 15
Records in the table:
|2||Priya Krishna||New Delhi||India|
|4||Michael Scott||New York||USA|
|11||Justin Green||Ottawa City||Canada|
SQL syntax to demonstrate the basic function of the TOP keyword
SELECT TOP 2 * FROM customers;
Number of records: 2
In the above example, we can see that the TOP keyword with the SELECT clause returned just the top 2 records from the customer’s data table.
SQL syntax to demonstrate the function of the TOP keyword when selecting specific columns only
SELECT TOP 3 ID, Customer FROM customers;
Number of records: 3
SQL syntax to demonstrate the function of the TOP keyword along with WHERE clause
SELECT TOP 3 * FROM customers WHERE Country= ‘England’;
Number of records: 3
In the above example, the query returned the first 3 records where the country was England.
SQL syntax to demonstrate the function of the TOP keyword along with WHERE and ORDER BY clause.
SELECT TOP 3 * FROM customers WHERE Country= ‘England’ ORDER BY Customer DESC;
Number of records: 3
In the above example, the query returned the first 3 records after sorting the resultant set in decreasing order by customer names.
SQL syntax to demonstrate the function of the TOP keyword along with the PERCENT and ORDER BY clause.
SELECT TOP 50 PERCENT * FROM Customers ORDER BY Customer ASC;
Number of records: 8
The query in the above example returned the top 50% of the total resultant recordset after sorting them in ascending order by customer name.
TOP is a very useful keyword when it comes to getting the few records from an ordered set. For example, we want the first 3 records or the last 3 records, etc. for an ascending or descending result set respectively.
This is a guide to TOP in SQL. Here we discuss the query examples of TOP in SQL along with the appropriate syntax and the parameters. You can also go through our other related articles to learn more–