The SELECT TOP clause specifies the quantity of records to be retrieved.
The SELECT TOP clause is beneficial for optimizing performance on large tables with thousands of records, as retrieving a large number of records can affect performance.
Retrieve solely the initial 3 records from the Customers table.
SELECT TOP 3 * FROM Customers; |
Note: Not all database systems support the SELECT TOP clause. MySQL employs the LIMIT clause to select a specific number of records, while Oracle uses FETCH FIRST n ROWS ONLY and ROWNUM to achieve the same result. |
SQL Server / MS Access Syntax:
SELECT TOP number|percent column_name(s) |
MySQL Syntax:
SELECT column_name(s) |
Oracle 12 Syntax:
SELECT column_name(s) |
Older Oracle Syntax:
SELECT column_name(s) |
Older Oracle Syntax (with ORDER BY):
SELECT * |
Here is an excerpt from the Customers table referenced in the examples:
CustomerID |
CustomerName |
ContactName |
Address |
City |
PostalCode |
Country |
1 |
Alfreds Futterkiste |
Maria Anders |
Obere Str. 57 |
Berlin |
12209 |
Germany |
2 |
Ana Trujillo Emparedados y helados |
Ana Trujillo |
Avda. de la Constitución 2222 |
México D.F. |
05021 |
Mexico |
3 |
Antonio Moreno Taquería |
Antonio Moreno |
Mataderos 2312 |
México D.F. |
05023 |
Mexico |
4 |
Around the Horn |
Thomas Hardy |
120 Hanover Sq. |
London |
WA1 1DP |
UK |
5 |
Berglunds snabbköp |
Christina Berglund |
Berguvsvägen 8 |
Luleå |
S-958 22 |
Sweden |
The following SQL statement demonstrates the equivalent example in MySQL:
SELECT * FROM Customers LIMIT 3; |
The following SQL statement illustrates the equivalent example in Oracle:
Retrieve the initial 3 records from the Customers table.
SELECT * FROM Customers FETCH FIRST 3 ROWS ONLY; |
The provided SQL statement selects the top 50% of records from the “Customers” table (for SQL Server/MS Access).
SELECT TOP 50 PERCENT * FROM Customers; |
The following SQL statement demonstrates the equivalent example in Oracle:
SELECT * FROM Customers FETCH FIRST 50 PERCENT ROWS ONLY; |
The SQL statement below selects the initial three records from the “Customers” table, specifically those where the country is “Germany” (for SQL Server/MS Access):
SELECT TOP 3 * FROM Customers WHERE Country=‘Germany’; |
The following SQL statement illustrates the equivalent example in MySQL:
SELECT * FROM Customers |
The following SQL statement illustrates the equivalent example for Oracle:
SELECT * FROM Customers WHERE Country=‘Germany’ FETCH FIRST 3 ROWS ONLY; |
In SQL Server and MS Access, incorporate the “ORDER BY” clause to arrange the results and retrieve the top 3 records from the sorted outcome.
Arrange the result in reverse alphabetical order based on CustomerName, and retrieve the top 3 records.
SELECT TOP 3 * FROM Customers ORDER BY CustomerName DESC; |
The subsequent SQL statement provides an equivalent example tailored for MySQL:
SELECT * FROM Customers ORDER BY CustomerName DESC LIMIT 3; |
SELECT * FROM Customers ORDER BY CustomerName DESC FETCH FIRST 3 ROWS ONLY; |