7.6.
LIMIT
and
OFFSET
7.6.
LIMIT
and
OFFSET
LIMIT
and
OFFSET
allow you to retrieve just a portion of the rows that are generated by the rest
of the query:
LIMIT and OFFSET allow to retrieve only part of the line generated by the query:
SELECT select_list
FROM table_expression
[ ORDER BY ... ]
[ LIMIT { number | ALL } ] [ OFFSET number ]
If a limit count is given, no more than that many rows will be returned (but possibly fewer, if the
query itself yields fewer rows).
LIMIT ALL
is the same as omitting the
LIMIT
clause, as is
LIMIT
with a NULL argument.
If you limit the number of output, then the return line is not more than the limit (but query returns rows than the limit is low, it may be less than the limit).
ALL LIMIT LIMIT clause omitted with the same, similar LIMIT plus NULL parameter.
OFFSET
says to skip that many rows before beginning to return rows.
OFFSET 0
is the same as
omitting the
OFFSET
clause, as is
OFFSET
with a NULL argument.
Specifies the number of rows OFFSET return result rows skipped. OFFECT 0 OFFECT omitted and the same, similar OFFSET add parameter NULL.
If both
OFFSET
and
LIMIT
appear, then
OFFSET
rows are skipped before starting to count the
LIMIT
rows that are returned.
Meanwhile, if the specified OFFSET and LIMIT, then skip OFFSET specified row, and then returns the number of rows is defined LIMIT.
When using
LIMIT
, it is important to use an
ORDER BY
clause that constrains the result rows into a
unique order. Otherwise you will get an unpredictable subset of the query's rows. You might be asking
for the tenth through twentieth rows, but tenth through twentieth in what ordering? The ordering is
unknown, unless you specified
ORDER BY
.
When using LIMIT, ORDER BY clause is the control output line is very important to sort.
Otherwise, you get unpredictable subset of the query line. Let's say you may want to tenth to the twentieth row, but in what order it queries the tenth to the twentieth row unless you specify ORDER BY, otherwise the order is unknown.
The query optimizer takes
LIMIT
into account when generating query plans, so you are very likely
to get different plans (yielding different row orders) depending on what you give for
LIMIT
and
OFFSET
. Thus, using different
LIMIT
/
OFFSET
values to select different subsets of a query result
will give inconsistent results
unless you enforce a predictable result ordering with
ORDER BY
. This
is not a bug; it is an inherent consequence of the fact that SQL does not promise to deliver the results
of a query in any particular order unless
ORDER BY
is used to constrain the order.
When the query optimizer generates a query plan will consider LIMIT, thus depending on the LIMIT and OFFSET, you are likely to get a different execution plan (to produce different row order). So, unless you use ORDER BY forced to get a predictable result ordering, otherwise use different subsets of different LIMIT / OFFSET values to select query result will produce inconsistent results. This is not a defect; because unless you use ORDER BY restraint order, or SQL query results are not guaranteed to be delivered in any particular order, this is a convention.
The rows skipped by an
OFFSET
clause still have to be computed inside the server; therefore a large
OFFSET
might be inefficient.
Using rows skipped OFFSET clause still inside the server must be calculated; therefore, such a large OFFSET can be inefficient.