]> begriffs open source - ai-pg/blob - full-docs/txt/queries-limit.txt
Convert HTML docs to more streamlined TXT
[ai-pg] / full-docs / txt / queries-limit.txt
1
2 7.6. LIMIT and OFFSET #
3
4    LIMIT and OFFSET allow you to retrieve just a portion of the rows that
5    are generated by the rest of the query:
6 SELECT select_list
7     FROM table_expression
8     [ ORDER BY ... ]
9     [ LIMIT { count | ALL } ]
10     [ OFFSET start ]
11
12    If a limit count is given, no more than that many rows will be returned
13    (but possibly fewer, if the query itself yields fewer rows). LIMIT ALL
14    is the same as omitting the LIMIT clause, as is LIMIT with a NULL
15    argument.
16
17    OFFSET says to skip that many rows before beginning to return rows.
18    OFFSET 0 is the same as omitting the OFFSET clause, as is OFFSET with a
19    NULL argument.
20
21    If both OFFSET and LIMIT appear, then OFFSET rows are skipped before
22    starting to count the LIMIT rows that are returned.
23
24    When using LIMIT, it is important to use an ORDER BY clause that
25    constrains the result rows into a unique order. Otherwise you will get
26    an unpredictable subset of the query's rows. You might be asking for
27    the tenth through twentieth rows, but tenth through twentieth in what
28    ordering? The ordering is unknown, unless you specified ORDER BY.
29
30    The query optimizer takes LIMIT into account when generating query
31    plans, so you are very likely to get different plans (yielding
32    different row orders) depending on what you give for LIMIT and OFFSET.
33    Thus, using different LIMIT/OFFSET values to select different subsets
34    of a query result will give inconsistent results unless you enforce a
35    predictable result ordering with ORDER BY. This is not a bug; it is an
36    inherent consequence of the fact that SQL does not promise to deliver
37    the results of a query in any particular order unless ORDER BY is used
38    to constrain the order.
39
40    The rows skipped by an OFFSET clause still have to be computed inside
41    the server; therefore a large OFFSET might be inefficient.