MySQL LIMIT

The MySQL LIMIT clause is used to narrow or limit, a result set to the specified number of rows.

The LIMIT clause accepts one or two arguments, one is offset and another one is count.

Offset: It is used to specify the offset of the first row to be returned.
Count: It is used to specify the maximum number of rows to be returned.

Syntax:

SELECT column_name(s) FROM  table_name LIMIT offset_number, count;

SELECT * FROM table_name LIMIT offset,count;

SELECT * FROM table_name LIMIT 10;

The query above will limit the result set to 10 rows.

When we will use argument in the limit clause, this argument will be used to return the maximum number of rows to be returned from the beginning of the result set.

SELECT column_name FROM table_name LIMIT count;

The above ex equivalent to this ex with the LIMIT clause that accepts two arguments.

SELECT column_name FROM table_name  LIMIT 0, count;

How to use OFFSET in the LIMIT Query.

The OFFSET value allows specifying which row to start from retrieving data.

Note: The OFFSET for the first row is 0 and not 1.

let’s suppose that if you want to retrieve data starting from the middle of the rows, then you can use the LIMIT keyword together with the OFFSET value to achieve that.

Table tblstudents

idNameRoll_noClass
1Anuj kumar10806121B.tech
2Sanjeev10883327MBA
3Amit13123121BA
4Rahul23424323LLB

Ex:

Select * from tblstudents limit 2;

The query above will limit the result set to 2rows.

Output

id Name Roll_no Class
1 Anuj kumar 10806121 B.tech
2 Sanjeev 10883327 MBA

Ex:

select * from tblstudents limit 2,2;

The query above has OFFSET=2, Hence 3rd row is return and LIMIT=2, Hence only 2 record will return

Output

id Name Roll_no Class
3 Amit 13123121 BA
4 Rahul 23424323 LLB
Leave A Reply

Your email address will not be published.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More

Privacy & Cookies Policy