Retrieving Top Records with SQL SELECT TOP

When working with large datasets, you often need to retrieve only a subset of the data, such as the top n records. This is especially useful for reports, leaderboards, and situations where displaying all the data would be impractical or overwhelming. SQL provides a simple mechanism for this through the SELECT TOP clause, which allows you to limit the number of rows returned by a query.

In this chapter, we’ll explore how to use the SELECT TOP clause, examine its variations across different database systems (like MySQL, SQL Server, PostgreSQL, and Oracle), and understand how to work with sorting and filtering when retrieving top records.

Basic Syntax of SQL SELECT TOP

The SELECT TOP clause is primarily used in SQL Server and MS Access to limit the number of rows returned by a query. The basic syntax is:

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

				
			
  • number: Specifies the exact number of records you want to retrieve.
  • percent: If you use this, it retrieves the top n% of rows.
  • ORDER BY: Used to specify how the records should be sorted (typically in descending or ascending order).

Example 1: Retrieving the Top 3 Records

Consider a table employees:

IDNameDepartmentSalary
1John DoeHR5000
2Jane SmithIT6000
3David GreenSales7000
4Sarah BrownIT8000
5James WhiteSales5500

To retrieve the top 3 employees with the highest salary:

Query:

				
					SELECT TOP 3 name, salary
FROM employees
ORDER BY salary DESC;

				
			

Output:

NameSalary
Sarah Brown8000
David Green7000
Jane Smith6000

The query returns the top 3 employees, sorted by salary in descending order.

Using SELECT TOP with PERCENT

In some cases, you may not want to retrieve a fixed number of rows but rather a percentage of rows from the total dataset. The PERCENT keyword allows you to do this.

Example 2: Retrieving the Top 20% of Records

If you want to retrieve the top 20% of employees based on salary:

Query:

				
					SELECT TOP 20 PERCENT name, salary
FROM employees
ORDER BY salary DESC;

				
			

Output:

NameSalary
Sarah Brown8000

The query returns the top 20% of employees based on their salary. Since there are only 5 employees, 20% of 5 is 1, so only 1 record is returned.

Working with SQL SELECT TOP and WHERE Clause

You can combine the TOP clause with the WHERE clause to filter records before limiting the number of rows returned.

Example 3: Filtering and Retrieving Top Records

Let’s say you want to find the top 2 employees from the IT department based on their salary.

Query:

				
					SELECT TOP 2 name, salary
FROM employees
WHERE department = 'IT'
ORDER BY salary DESC;

				
			

Output:

NameSalary
Sarah Brown8000
Jane Smith6000

The query first filters for employees in the IT department and then retrieves the top 2 based on salary.

Using SELECT TOP with Complex Sorting

You can use the ORDER BY clause in more complex ways to sort based on multiple columns. For example, you can first sort by department and then by salary within each department.

Example 4: Sorting by Multiple Columns

Retrieve the top 3 employees sorted by department first and then by salary.

Query:

				
					SELECT TOP 3 name, department, salary
FROM employees
ORDER BY department ASC, salary DESC;

				
			

Output:

NameDepartmentSalary
John DoeHR5000
Sarah BrownIT8000
Jane SmithIT6000

The query first orders by department in ascending order (HR first, then IT), and then within each department, it orders by salary in descending order.

Retrieving Top Records in Other Databases

The SELECT TOP clause is specific to SQL Server and MS Access. Other databases like MySQL, PostgreSQL, and Oracle use different approaches to achieve the same functionality. Let’s explore these.

MySQL: Using LIMIT

In MySQL, the LIMIT clause is used instead of TOP.

Example in MySQL:

				
					SELECT name, salary
FROM employees
ORDER BY salary DESC
LIMIT 3;

				
			

This query retrieves the top 3 employees with the highest salary.

PostgreSQL: Using LIMIT

Similarly, PostgreSQL also uses LIMIT to retrieve a specific number of rows.

Example in PostgreSQL:

				
					SELECT name, salary
FROM employees
ORDER BY salary DESC
LIMIT 3;

				
			

Oracle: Using ROWNUM or FETCH FIRST

Oracle uses ROWNUM or the FETCH FIRST clause for limiting the result set.

Example in Oracle:

				
					SELECT name, salary
FROM employees
ORDER BY salary DESC
FETCH FIRST 3 ROWS ONLY;

				
			

This query retrieves the top 3 employees in Oracle.

Handling Ties in Top Records

When retrieving top records, you may encounter ties, where two or more rows have the same value in the sorting column. By default, SQL Server arbitrarily chooses which rows to include in the result set when ties occur. However, if you want to explicitly handle ties, you can use the WITH TIES option.

Example 5: Using SELECT TOP WITH TIES

Let’s assume the employees table has another record with a salary of 8000, causing a tie.

Query:

				
					SELECT TOP 2 WITH TIES name, salary
FROM employees
ORDER BY salary DESC;

				
			

Output:

NameSalary
Sarah Brown8000
John Black8000

The query returns both employees with a salary of 8000 because of the WITH TIES option.

Combining SELECT TOP with OFFSET (Pagination)

In some cases, you may want to implement pagination, where users can view data in chunks (e.g., 10 records at a time). The combination of SELECT TOP and OFFSET is useful in SQL Server for pagination.

Example 6: Using OFFSET for Pagination

To retrieve records from the 6th row to the 10th row, you can use the following query:

				
					SELECT name, salary
FROM employees
ORDER BY salary DESC
OFFSET 5 ROWS FETCH NEXT 5 ROWS ONLY;

				
			

Output:

NameSalary

The query retrieves the next 5 rows after skipping the first 5 rows.

Performance Considerations

While using SELECT TOP is a powerful way to retrieve a subset of rows, there are some performance considerations to keep in mind:

  • Indexes and Sorting: When using TOP with an ORDER BY clause, the query engine needs to sort the data first. Ensure that the columns used in the ORDER BY clause are indexed to improve performance.

  • Large Datasets: For very large datasets, retrieving even the top n records can be resource-intensive if the database needs to scan and sort a significant number of rows.

  • Use of LIMIT (in MySQL/PostgreSQL): The use of LIMIT in MySQL or PostgreSQL should also be considered in combination with indexed columns for optimal performance.

The SELECT TOP clause is an essential feature in SQL for retrieving a limited number of records from a dataset. From basic retrieval of top n rows to more complex scenarios involving percentages, filtering, and sorting, SELECT TOP is a versatile tool. It’s important to understand the nuances of its implementation in different database systems, such as MySQL’s LIMIT and Oracle’s FETCH FIRST. Happy coding !❤️

Table of Contents

Contact here

Copyright © 2025 Diginode

Made with ❤️ in India