MySQL – Difference Between ‘LIMIT 0, 1’ and ‘LIMIT 1’

MySQL

I recently stumbled upon example codes, which differed by these notations.

SELECT * FROM table LIMIT 0, 1
SELECT * FROM table LIMIT 1

The first argument should be considered as the offset if I'm not wrong, the purpose of those two queries is to limit the selection to up the first row of table

Is there any negative effect when leaving out the offset/how is it possible to leave it out in the first place? Or did I misunderstood the queries?

Best Answer

As the documentation says:

The LIMIT clause can be used to constrain the number of rows returned by the SELECT statement. LIMIT takes one or two numeric arguments, which must both be nonnegative integer constants (except when using prepared statements).

With two arguments, the first argument specifies the offset of the first row to return, and the second specifies the maximum number of rows to return. The offset of the initial row is 0 (not 1).

The LIMIT clause is used in the SELECT statement to constrain the number of rows in a result set. The LIMIT clause accepts one or two arguments. The values of both arguments must be zero or positive integer constants.

The following illustrates the LIMIT clause syntax with 2 arguments:

SELECT * FROM tbl
LIMIT offset, count;

Let’s see what the offset and count mean in the LIMIT clause:

  • The offset specifies the offset of the first row to return. The offset of the first row is 0, not 1.
  • The count specifies maximum number of rows to return.

When you use LIMIT with one argument, this argument will be used to specifies the maximum number of rows to return from the beginning of the result set.

SELECT * FROM tbl LIMIT count;

The query above is equivalent to the following query with the LIMIT clause that accepts two arguments:

SELECT * FROM tbl LIMIT 0, count;

The LIMIT clause often used with ORDER BY clause. First, you use the ORDER BY clause to sort the result set based on a certain criteria, and then you use LIMIT clause to find lowest or highest values.