SELECT with a LIMIT clause

suggest change

Query:

SELECT *
  FROM Customers
 ORDER BY CustomerID 
 LIMIT 3;

Result:

+------------+------------------------------------+-------------+---------------------------+-------------+------------+---------+
| 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  |
+------------+------------------------------------+-------------+---------------------------+-------------+------------+---------+

Best Practice Always use ORDER BY when using LIMIT; otherwise the rows you will get will be unpredictable.

Query:

SELECT *
  FROM Customers
 ORDER BY CustomerID 
 LIMIT 2,1;

Explanation:

When a LIMIT clause contains two numbers, it is interpreted as LIMIT offset,count. So, in this example the query skips two records and returns one.

Result:

+------------+-------------------------+----------------+----------------+-------------+------------+---------+
| CustomerID | CustomerName            | ContactName    | Address        | City        | PostalCode | Country |
+------------+-------------------------+----------------+----------------+-------------+------------+---------+
|          3 | Antonio Moreno Taquería | Antonio Moreno | Mataderos 2312 | México D.F. | 05023      | Mexico  |
+------------+-------------------------+----------------+----------------+-------------+------------+---------+

Note:

LIMIT arguments cannot be column values. They can be integer constants or, in a stored program, integer variables.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents