MOCKSTACKS
EN
Questions And Answers

More Tutorials









MYSQL Limit and Offset

Limit and Offset relationship


Considering the following users table:

id username


1 User1
2 User2
3 User3
4 User4
5 User5

In order to constrain the number of rows in the result set of a SELECT query, the LIMIT clause can be used together with one or two positive integers as arguments (zero included).

LIMIT clause with one argument


When one argument is used, the result set will only be constrained to the number specified in the following manner:

SELECT * FROM users ORDER BY id ASC LIMIT 2

id username


1 User1
2 User2

If the argument's value is 0, the result set will be empty.

Also notice that the ORDER BY clause may be important in order to specify the first rows of the result set that will be presented (when ordering by another column).

LIMITclause with two arguments


When two arguments are used in a LIMIT clause:

.the first argument represents the row from which the result set rows will be presented – this number is
often mentioned as an offset, since it represents the row previous to the initial row of the constrained result
set. This allows the argument to receive 0 as value and thus taking into consideration the first row of the nonconstrained result set.

.the second argument specifies the maximum number of rows to be returned in the result set (similarly to
the one argument's example).


Therefore the query:

SELECT * FROM users ORDER BY id ASC LIMIT 2, 3

Presents the following result set:

id username


3 User3
4 User4
5 User5


Conclusion

In this page (written and validated by ) you learned about MYSQL Limit and Offset . What's Next? If you are interested in completing MYSQL tutorial, your next topic will be learning about: MYSQL Creating databases.



Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue: mockstacks@gmail.com.


Share On:


Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using Mockstacks.com, you agree to have read and accepted our terms of use, cookies and privacy policy.