MySQL SQL

MySQL LIMIT

The MySQL LIMIT Clause

The number of records to be returned is specified using the LIMIT clause.

For huge tables with thousands of records, the LIMIT clause is helpful. Performance may suffer if a lot of records are returned.

LIMIT Syntax

				
					SELECT column_name(s)
FROM table_name
WHERE condition
LIMIT number;
				
			

Demo Database

A sample from the “Customers” table in the Northwind sample database is shown below:

CustomerIDCustomerNameContactNameAddressCityPostalCodeCountry
1

Alfreds FutterkisteMaria AndersObere Str. 57Berlin12209Germany
2Ana Trujillo Emparedados y heladosAna TrujilloAvda. de la Constitución 2222México D.F.05021Mexico
3Antonio Moreno TaqueríaAntonio MorenoMataderos 2312México D.F.05023Mexico
4

Around the HornThomas Hardy120 Hanover Sq.LondonWA1 1DPUK
5Berglunds snabbköpChristina BerglundBerguvsvägen 8LuleåS-958 22Sweden

MySQL LIMIT Examples

The following SQL statement selects the first three records from the “Customers” table:

Example

				
					SELECT * FROM Customers
LIMIT 3;
				
			

What happens if we wish to pick records 4 through 6 (inclusive)?

MySQL offers an OFFSET-based solution for this.

“Return only 3 records, start on record 4 (OFFSET 3)” is what the SQL query below states.

Example

				
					SELECT * FROM Customers
LIMIT 3 OFFSET 3;
				
			

ADD a WHERE CLAUSE

With “Germany” as the nation, the first three records in the “Customers” table are chosen via the SQL statement that follows:

Example

				
					SELECT * FROM Customers
WHERE Country='Germany'
LIMIT 3;
				
			
Share this Doc

MySQL LIMIT

Or copy link

Explore Topic