bugl
bugl
HomeLearnPatternsSearch
HomeLearnPatternsSearch

Loading lesson path

Learn/SQL/SQL Tutorial
SQL•SQL Tutorial

SQL SELECT TOP, LIMIT and FETCH FIRST

Concept visual

SQL SELECT TOP, LIMIT and FETCH FIRST

Pointer walk
two pointers
leftright102132436485116
left=0
right=6
1
3

Start at both ends

The SQL SELECT TOP Clause

The SELECT TOP

clause is used to limit the number of records to return.

The SELECT TOP

clause is useful on large tables with thousands of records. Returning a large number of records can impact performance. The following SQL selects only the first 3 records of the "Customers" table:

Example

Select only the first 3 records of the Customers table:

SELECT TOP 3 * FROM Customers;

Note:

Not all database systems support the SELECT TOP clause. MySQL supports the LIMIT clause to select a limited number of records, while Oracle uses

Fetch First

n ROWS ONLY.

Formula

Syntax for SQL Server / MS Access

Select Top

number | percent column_name(s)

From

table_name

Where

condition

;

Syntax for MySQL

Select

column_name(s)

From

table_name

Where

condition

Limit

number

;

Syntax for Oracle 12+

Select

column_name(s)

From

table_name

Order By

column_name(s)

Fetch First

number

ROWS ONLY;

Demo Database

Below is a selection from the

Customers table used in the examples:

CustomerID

CustomerName

ContactName

Address

Previous

SQL DELETE Statement

Next

SQL Aggregate Functions