LIMIT

Restrict the number of rows returned. This clause specifies the maximum number of records to return and can optionally set an offset.

Description

Use the LIMIT clause to restrict the number of returned rows. When you use a single integer n with LIMIT, the first n rows will be returned. Use the ORDER BY clause to control which rows come first. You can also select a number of rows after an offset using either of the following:

LIMIT offset, row_count
LIMIT row_count OFFSET offset

When you provide an offset m with a limit n, the first m rows will be ignored, and the following n rows will be returned.

Executing an UPDATE with the LIMIT clause is not safe for replication. LIMIT 0 is an exception to this rule (see MDEV-6170).

There is a LIMIT ROWS EXAMINED optimization which provides the means to terminate the execution of SELECT statements which examine too many rows, and thus use too many resources. See LIMIT ROWS EXAMINED.

Multi-Table Updates

It is possible to use LIMIT (or ORDER BY) in a multi-table UPDATE statement.

GROUP_CONCAT

It is possible to use LIMIT with GROUP_CONCAT().

Examples

CREATE TABLE members (name VARCHAR(20));
INSERT INTO members VALUES('Jagdish'),('Kenny'),('Rokurou'),('Immaculada');

SELECT * FROM members;
+------------+
| name       |
+------------+
| Jagdish    |
| Kenny      |
| Rokurou    |
| Immaculada |
+------------+

Select the first two names (no ordering specified):

All the names in alphabetical order:

The first two names, ordered alphabetically:

The third name, ordered alphabetically (the first name would be offset zero, so the third is offset two):

From MariaDB 10.3.2, LIMIT can be used in a multi-table update:

When using LIMIT with GROUP_CONCAT, you can simplify certain queries. Consider this table:

The following query works fine, but is rather complex:

It can be simplified to this:

See Also

This page is licensed: CC BY-SA / Gnu FDL

Last updated

Was this helpful?