MySQL clause LIMIT

The LIMIT clause provides by MySQL used to specify the number of records to return.

Syntax:

SELECT * FROM {table_name} LIMIT 5;

If we want to select records from 1 to 5 from a table "Employee". Our query would then look like this.

Example:

SELECT * FROM Employee LIMIT 5;

When the above query run, it will return the first 5 records.

NOTE: LIMIT clause accepts one or two numeric arguments, both arguments must be non-negative integer constants means zero or positive integer constants.

LIMIT clause syntax with 2 arguments:

SELECT * FROM {table_name} LIMIT offset, count;

Example:

SELECT * FROM Employee LIMIT 0, 5;

offset and count means in the LIMIT clause.

Offset specifies the offset of the first row to return. The offset of the first row is 0, not 1.

Count specifies maximum number of rows to return.

NOTE: IF we 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.

With one argument, the value specifies the number of rows to return from the beginning of the result set.

SELECT * FROM {table_name} LIMIT 5;

In above query we retrieve first 5 rows.

In other words, LIMIT row_count is equivalent to LIMIT 0, row_count.

SELECT * FROM {table_name} LIMIT 5;

is equivalent to

SELECT * FROM {table_name} LIMIT 0, 5;

Example:

SELECT * FROM Employee LIMIT 5;

is equivalent to

SELECT * FROM Employee LIMIT 0, 5;

If we want to retrieve records from row 6 to 10, the uery would be...

SELECT * FROM {table_name} LIMIT 5, 5;

Example:

SELECT * FROM Employee LIMIT 5, 5;
Tags