MySQL LIMIT

0

- Advertisement -

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 * FROM table_name LIMIT offset,count;

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.

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

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:

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:

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

- Advertisement -

Leave A Reply

Your email address will not be published.