TOP

SQL TOP, LIMIT, FETCH FIRST and ROWNUM

YouLibreCalc for Excel logo

SQL SELECT TOP Description

The expression SELECT TOP is used to specify the number of records to select.

SQL SELECT TOP is useful for large tables with thousands of records, as returning a large number of records can affect system performance.


Not all database systems support the SELECT TOP expression. MySQL supports the expression LIMIT to select a limited number of records, while Oracle uses FETCH FIRST n ROWS ONLY and ROWNUM.

SQL Server / MS Access Syntax:

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

MySQL Syntax:

SELECT column_name(s)
FROM table_name
WHERE condition
LIMIT number

Oracle 12 Syntax:

SELECT column_name(s)
FROM table_name
ORDER BY column_name(s)
FETCH FIRST number ROWS ONLY

Deprecated Oracle Syntax:

SELECT column_name(s)
FROM table_name
WHERE ROWNUM <= number

Obsolete Oracle Syntax (with ORDER BY):

SELECT *
FROM (SELECT column_name(s) FROM table_name ORDER BY column_name(s))
WHERE ROWNUM <= number

Demonstration database

The following is a sample from the "Customers" ("Customers") table of the "Northwind" database:

CustomerIDCustomerNameContactNameAddressCityPostalCodeCountry
1Alfreds FutterkisteMaria AndersObere Str. 57Berlin12209Germany
2Ana Trujillo Emparedados y heladosAna TrujilloAvda. de la Constitución 2222México D.F.5021Mexico
3Antonio Moreno TaqueríaAntonio MorenoMataderos 2312México D.F.5023Mexico
4Around the HornThomas Hardy120 Hanover Sq.LondonWA1 1DPUK
5Berglunds snabbköpChristina BerglundBerguvsvägen 8LuleåS-958 22Sweden

SQL TOP, LIMIT and FETCH FIRST Examples

The following SQL statement selects the first three records from the "Customers" table (for SQL Server/MS Access):

SELECT TOP 3 * 
FROM Customers

The following SQL statement shows an equivalent example for MySQL:

Run SQLSELECT * 
FROM Customers 
LIMIT 3

The following SQL statement shows an equivalent example for Oracle:

SELECT * 
FROM Customers 
FETCH FIRST 3 ROWS ONLY

SQL TOP PERCENT Example

The following SQL statement selects the first 50% of records from the "Customers" table (for SQL Server/MS Access):

SELECT TOP 50 PERCENT * 
FROM Customers

The following SQL statement shows an equivalent example for Oracle:

SELECT * 
FROM Customers 
FETCH FIRST 50 PERCENT ROWS ONLY

Using the WHERE expression

The following SQL statement selects the first three records from the table "Customers" where the country is "Germany" (for SQL Server/MS Access):

SELECT TOP 3 * 
FROM Customers 
WHERE Country = 'Germany'

The following SQL statement shows an equivalent example for MySQL:

Run SQLSELECT * 
FROM Customers 
WHERE Country = 'Germany' 
LIMIT 3

The following SQL statement shows an equivalent example for Oracle:

SELECT * 
FROM Customers 
WHERE Country = 'Germany' 
FETCH FIRST 3 ROWS ONLY