MySQL SQL
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.
SELECT column_name(s)
FROM table_name
WHERE condition
LIMIT number;
A sample from the “Customers” table in the Northwind sample database is shown below:
| 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 selects the first three records from the “Customers” table:
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.
SELECT * FROM Customers
LIMIT 3 OFFSET 3;
With “Germany” as the nation, the first three records in the “Customers” table are chosen via the SQL statement that follows:
SELECT * FROM Customers
WHERE Country='Germany'
LIMIT 3;
CodingAsk.com is designed for learning and practice. Examples may be made simpler to aid understanding. Tutorials, references, and examples are regularly checked for mistakes, but we cannot guarantee complete accuracy. By using CodingAsk.com, you agree to our terms of use, cookie, and privacy policy.
Copyright 2010-2024 by Refsnes Data. All Rights Reserved.