Menu

SQL SELECT TOP – Diving Deep into the ‘SELECT TOP’ Clause

Written by Jagdeesh | 2 min read

SELECT TOP is an essential clause for developers and database administrators, allowing for the efficient and strategic handling of voluminous data.

Understanding SELECT TOP

The SELECT TOP clause is a SQL command used to limit the number of rows returned from a query. This is particularly useful when you’re dealing with large databases, and you want a subset of records. For example, you may want to fetch the top 10 performers in a sales team, the top 100 customers by sales volume, or the first 50 records from a table.

The SELECT TOP clause is supported by SQL Server (Transact-SQL). In MySQL, the LIMIT clause provides similar functionality, and in Oracle SQL, the ROWNUM clause serves the same purpose. But today, we’re focusing on the SELECT TOP clause as used in SQL Server.

The basic syntax is as follows

sql
SELECT TOP number|percent column_name(s)
FROM table_name
WHERE condition;

Where:

  • number|percent: Defines the number or percentage of records to return.
  • column_name(s): The column(s) you want to select from your database.
  • table_name: The name of the table you want to select data from.
  • WHERE condition: An optional part specifying conditions that must be fulfilled for the records to be selected.

Let’s dive into an example

To give you a practical understanding of how this works, let’s consider an example dataset, Orders, with the following structure

output
| OrderID | CustomerID | OrderDate | TotalAmount |
|---------|------------|-----------|-------------|
| 1       | 101        |2023-01-01 | 300.00      |
| 2       | 102        |2023-01-02 | 200.00      |
| 3       | 103        |2023-01-03 | 150.00      |
| 4       | 104        |2023-01-04 | 250.00      |
| 5       | 105        |2023-01-05 | 350.00      |

Fetching Top N Records

If you want to select the top 3 orders based on the TotalAmount, you can use the following query

sql
SELECT TOP 3 * 
FROM Orders 
ORDER BY TotalAmount DESC;

This query will return

output
| OrderID | CustomerID | OrderDate | TotalAmount |
|---------|------------|-----------|-------------|
| 5       | 105        |2023-01-05 | 350.00      |
| 1       | 101        |2023-01-01 | 300.00      |
| 4       | 104        |2023-01-04 | 250.00      |

The ORDER BY TotalAmount DESC clause sorts the records in descending order by the TotalAmount before the TOP clause limits the result set to 3 records.

Fetching a Percentage of Records

You can also fetch a percentage of records using the PERCENT keyword. For instance, to fetch the top 50 percent orders based on the TotalAmount, you’d use

sql
SELECT TOP 50 PERCENT * 
FROM Orders 
ORDER BY TotalAmount DESC;

This will return the top 50 percent of the records, sorted by TotalAmount in descending order.

Closing Thoughts

The SELECT TOP clause is a powerful and flexible tool in SQL, offering a way to retrieve a specific number or percentage of records from a database. It’s essential when dealing with large datasets, allowing you to filter and return only the most relevant data.

Remember, though, that the ‘top’ records will depend on how your data is ordered. It’s crucial to pair SELECT TOP with the ORDER BY clause to ensure your results are sorted in a meaningful way.

Free Course
Master Core Python — Your First Step into AI/ML

Build a strong Python foundation with hands-on exercises designed for aspiring Data Scientists and AI/ML Engineers.

Start Free Course
Trusted by 50,000+ learners
Jagdeesh
Written by
Related Course
Master SQL — Hands-On
Join 5,000+ students at edu.machinelearningplus.com
Explore Course
⚡ Before you go

Python.
SQL. NumPy.
All free.

Get the exact 10-course programming foundation that Data Science (AI/ML) professionals use and kick start your Data Science (AI/ML) Career.

🐍
Core Python — from first line of code to expert level
📈
NumPy & Pandas — the #1 libraries every DS job needs
🗀
SQL Levels I–III — from basics to Window Functions
📄
Real industry data — Jupyter notebooks included
R A M S K
57,000+ students taught by Selva Prabhakaran
★★★★★ Rated 4.9 / 5 — 200+ reviews
Get Free Access Now
10 courses. Real projects. Zero cost. No credit card.
New learners enrolling right now
🔒 100% free ☕ No spam, ever ✓ Instant access
🚀
You're in!
Check your inbox for your access link.
Or start your first course right now:
Start the free courses →
Scroll to Top
Scroll to Top
Course Preview

Machine Learning A-Z™: Hands-On Python & R In Data Science

Free Sample Videos:

Machine Learning A-Z™: Hands-On Python & R In Data Science

Machine Learning A-Z™: Hands-On Python & R In Data Science

Machine Learning A-Z™: Hands-On Python & R In Data Science

Machine Learning A-Z™: Hands-On Python & R In Data Science

Machine Learning A-Z™: Hands-On Python & R In Data Science