SQL Tutorial
SQL Clauses / Operators
SQL-Injection
SQL Functions
SQL Queries
PL/SQL
MySQL
SQL Server
Misc
The SELECT TOP
clause is used in SQL, especially within Microsoft's SQL Server, to limit the number of rows returned by a query. This is particularly useful when you want to retrieve a specific number of top records, such as the top 10 employees by salary or the first 5 orders placed.
SELECT TOP (number_or_percent) column1, column2, ... FROM table_name WHERE condition;
number_or_percent
: Specifies the number of records to return or a percentage of records to return.column1, column2, ...
: The columns you wish to retrieve from the table.Select Top N Rows:
SELECT TOP (5) first_name, last_name FROM employees;
This retrieves the first 5 employees based on the default ordering of the table (usually by the primary key).
Top N Rows with Ordering:
SELECT TOP (10) first_name, last_name, salary FROM employees ORDER BY salary DESC;
This retrieves the top 10 highest-paid employees.
Using a Percentage:
SELECT TOP (10) PERCENT first_name, last_name FROM employees ORDER BY hire_date;
This retrieves the earliest 10% of employees based on their hire dates.
The concept of limiting query results is present in other RDBMSs but with different syntax:
MySQL: Uses the LIMIT
clause.
SELECT first_name, last_name FROM employees LIMIT 5;
Oracle: Before Oracle 12c, you'd use the ROWNUM
condition. Starting from Oracle 12c, you can use the FETCH FIRST
clause.
SELECT first_name, last_name FROM employees FETCH FIRST 5 ROWS ONLY;
PostgreSQL: Uses the LIMIT
clause similar to MySQL.
When using SELECT TOP
without an ORDER BY
clause, the results can be somewhat arbitrary, especially when you're looking for meaningful top records, such as the highest salaries. Always use ORDER BY
for deterministic results.
Just like any query, performance can vary depending on the table size, indexes, and other factors. Always ensure your tables are properly indexed, especially when working with large datasets and performing ORDER BY
operations.
The SELECT TOP
clause is a powerful tool in SQL Server for fetching a limited number of records based on specific criteria, helping both in data analysis and application performance.
How to use SELECT TOP in SQL:
TOP
clause to retrieve a specified number of rows.SELECT TOP 5 column1, column2 FROM example_table;
Retrieving a specific number of rows with SELECT TOP:
TOP
.SELECT TOP 10 column1, column2 FROM example_table;
SELECT TOP vs. LIMIT in different database systems:
SELECT TOP
in SQL Server and LIMIT
in systems like MySQL or PostgreSQL.-- SQL Server SELECT TOP 5 column1, column2 FROM example_table; -- MySQL or PostgreSQL SELECT column1, column2 FROM example_table LIMIT 5;
Using SELECT TOP with ORDER BY in SQL:
TOP
with ORDER BY
to retrieve the top rows based on a specific column.SELECT TOP 5 column1, column2 FROM example_table ORDER BY column1 DESC;
Dynamic value for SELECT TOP in SQL:
DECLARE @top_value INT = 10; SELECT TOP (@top_value) column1, column2 FROM example_table;
SQL SELECT TOP without ORDER BY:
SELECT TOP 5 column1, column2 FROM example_table;
Limiting results with SELECT TOP in SQL Server:
TOP
in SQL Server.SELECT TOP 5 column1, column2 FROM example_table;
Getting the first N rows with SELECT TOP:
SELECT TOP 3 column1, column2 FROM example_table ORDER BY column1;
SELECT TOP in subqueries in SQL:
TOP
in a subquery to retrieve a specified number of rows.SELECT column1, column2 FROM example_table WHERE column1 IN (SELECT TOP 5 column1 FROM another_table);
Pagination using SELECT TOP in SQL:
TOP
value based on page and page size.DECLARE @page INT = 2; DECLARE @page_size INT = 10; SELECT TOP (@page_size) column1, column2 FROM example_table ORDER BY column1 OFFSET (@page - 1) * @page_size ROWS;
Differences between SELECT TOP and FETCH FIRST in SQL:
SELECT TOP
and FETCH FIRST
in various database systems.-- SQL Server SELECT TOP 5 column1, column2 FROM example_table; -- PostgreSQL SELECT column1, column2 FROM example_table ORDER BY column1 FETCH FIRST 5 ROWS ONLY;
Combining SELECT TOP with WHERE clause in SQL:
WHERE
in conjunction with TOP
.SELECT TOP 5 column1, column2 FROM example_table WHERE column3 = 'value';