gotchasqlMajor
Is there any difference between 'LIMIT 0, 1' and 'LIMIT 1'?
Viewed 0 times
limitanydifferencebetweenandthere
Problem
I recently stumbled upon example codes, which differed by these notations.
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
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?
SELECT * FROM table LIMIT 0, 1
SELECT * FROM table LIMIT 1The 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
tableIs 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?
Solution
As the documentation says:
The
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
The following illustrates the
Let’s see what the offset and count mean in the LIMIT clause:
When you use
The query above is equivalent to the following query with the
The
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
offsetspecifies the offset of the first row to return. The offset of the first row is 0, not 1.
- The
countspecifies 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 tblLIMIT count;The query above is equivalent to the following query with the
LIMIT clause that accepts two arguments:SELECT * FROM tblLIMIT 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.Code Snippets
SELECT * FROM tbl
LIMIT offset, count;Context
StackExchange Database Administrators Q#105850, answer score: 22
Revisions (0)
No revisions yet.