EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign Up
Home Data Science Data Science Tutorials SQL Tutorial TOP in SQL
 

TOP in SQL

Priya Pedamkar
Article byPriya Pedamkar

Updated March 27, 2023

TOP in SQL

 

 

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.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

Syntax: 

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)
expressions, column_name
FROM tables
[WHERE conditions]
[ORDER BY expression [ ASC | DESC ]];

Parameters

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

Customers
ID(primary key)
Customer
City
Country

Records in the table:

ID Customer City Country
1 Peter King Manchester England
2 Priya Krishna New Delhi India
3 Jim Halpert Manchester England
4 Michael Scott New York USA
5 Harvey Spector Birmingham England
6 Deepa Kamat Mumbai India
7 Anita Desai London England
8 Rachel Zane Michigan USA
9 Pretoria John Canberra Australia
10 John L Budapest Hungry
11 Justin Green Ottawa City Canada
12 Babita Ghosh Kolkata India
13 Krish Pratt London England
14 Elizabeth Blunt London England
15 Nina Debrov Amsterdam Netherlands

Example #1

SQL syntax to demonstrate the basic function of the TOP keyword

Code:

SELECT TOP 2 * FROM customers;

Output:

Number of records: 2

Top in SQL - Example 1.1

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.

Example #2

SQL syntax to demonstrate the function of the TOP keyword when selecting specific columns only

Code:

SELECT TOP 3 ID, Customer FROM customers;

Output:

Number of records: 3

Top in SQL - Example 1.2

Example #3

SQL syntax to demonstrate the function of the TOP keyword along with WHERE clause

Code:

SELECT TOP 3 * FROM customers WHERE Country= 'England';

Output:

Number of records: 3

WHERE Country = 'England'

In the above example, the query returned the first 3 records where the country was England.

Example #4

SQL syntax to demonstrate the function of the TOP keyword along with WHERE and ORDER BY clause.

Code:

SELECT TOP 3 * FROM customers WHERE Country= 'England' ORDER BY Customer DESC;

Output:

Number of records: 3

ORDER BY Customer DESC

In the above example, the query returned the first 3 records after sorting the resultant set in decreasing order by customer names.

Example #5

SQL syntax to demonstrate the function of the TOP keyword along with the PERCENT and ORDER BY clause.

Code:

SELECT TOP 50 PERCENT * FROM Customers ORDER BY Customer ASC;

Output:

Number of records: 8

Order BY Customer ASC

The query in the above example returned the top 50% of the total resultant recordset after sorting them in ascending order by customer name.

Note: All databases do not support the TOP keyword. It is specifically used in the SQL Server database. However, we can use other similar keywords such as LIMIT, ROWNUM, etc to perform the same functions. LIMIT works best with MYSQL and ROWNUM with the ORACLE database.

Conclusion

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.

Recommended Articles

We hope that this EDUCBA information on “TOP in SQL” was beneficial to you. You can view EDUCBA’s recommended articles for more information.

  1. LIKE Query in SQL
  2. MySQL Certification
  3. Types of NoSQL Databases
  4. ORDER BY in MySQL

Primary Sidebar

Footer

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA Login

Forgot Password?

🚀 Limited Time Offer! - 🎁 ENROLL NOW