summaryrefslogtreecommitdiffstats
path: root/doc/src/sgml/html/queries-limit.html
blob: 4236145cb109000d0533e3e477fa6f59a33a892f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>7.6. LIMIT and OFFSET</title><link rel="stylesheet" type="text/css" href="stylesheet.css" /><link rev="made" href="pgsql-docs@lists.postgresql.org" /><meta name="generator" content="DocBook XSL Stylesheets Vsnapshot" /><link rel="prev" href="queries-order.html" title="7.5. Sorting Rows (ORDER BY)" /><link rel="next" href="queries-values.html" title="7.7. VALUES Lists" /></head><body id="docContent" class="container-fluid col-10"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="5" align="center">7.6. <code class="literal">LIMIT</code> and <code class="literal">OFFSET</code></th></tr><tr><td width="10%" align="left"><a accesskey="p" href="queries-order.html" title="7.5. Sorting Rows (ORDER BY)">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="queries.html" title="Chapter 7. Queries">Up</a></td><th width="60%" align="center">Chapter 7. Queries</th><td width="10%" align="right"><a accesskey="h" href="index.html" title="PostgreSQL 15.4 Documentation">Home</a></td><td width="10%" align="right"> <a accesskey="n" href="queries-values.html" title="7.7. VALUES Lists">Next</a></td></tr></table><hr /></div><div class="sect1" id="QUERIES-LIMIT"><div class="titlepage"><div><div><h2 class="title" style="clear: both">7.6. <code class="literal">LIMIT</code> and <code class="literal">OFFSET</code></h2></div></div></div><a id="id-1.5.6.10.2" class="indexterm"></a><a id="id-1.5.6.10.3" class="indexterm"></a><p>
   <code class="literal">LIMIT</code> and <code class="literal">OFFSET</code> allow you to retrieve just
   a portion of the rows that are generated by the rest of the query:
</p><pre class="synopsis">
SELECT <em class="replaceable"><code>select_list</code></em>
    FROM <em class="replaceable"><code>table_expression</code></em>
    [<span class="optional"> ORDER BY ... </span>]
    [<span class="optional"> LIMIT { <em class="replaceable"><code>number</code></em> | ALL } </span>] [<span class="optional"> OFFSET <em class="replaceable"><code>number</code></em> </span>]
</pre><p>
  </p><p>
   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).
   <code class="literal">LIMIT ALL</code> is the same as omitting the <code class="literal">LIMIT</code>
   clause, as is <code class="literal">LIMIT</code> with a NULL argument.
  </p><p>
   <code class="literal">OFFSET</code> says to skip that many rows before beginning to
   return rows.  <code class="literal">OFFSET 0</code> is the same as omitting the
   <code class="literal">OFFSET</code> clause, as is <code class="literal">OFFSET</code> with a NULL argument.
  </p><p>
   If both <code class="literal">OFFSET</code>
   and <code class="literal">LIMIT</code> appear, then <code class="literal">OFFSET</code> rows are
   skipped before starting to count the <code class="literal">LIMIT</code> rows that
   are returned.
  </p><p>
   When using <code class="literal">LIMIT</code>, it is important to use an
   <code class="literal">ORDER BY</code> 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 <code class="literal">ORDER BY</code>.
  </p><p>
   The query optimizer takes <code class="literal">LIMIT</code> 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 <code class="literal">LIMIT</code> and <code class="literal">OFFSET</code>.  Thus, using
   different <code class="literal">LIMIT</code>/<code class="literal">OFFSET</code> values to select
   different subsets of a query result <span class="emphasis"><em>will give
   inconsistent results</em></span> unless you enforce a predictable
   result ordering with <code class="literal">ORDER BY</code>.  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
   <code class="literal">ORDER BY</code> is used to constrain the order.
  </p><p>
   The rows skipped by an <code class="literal">OFFSET</code> clause still have to be
   computed inside the server; therefore a large <code class="literal">OFFSET</code>
   might be inefficient.
  </p></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="queries-order.html" title="7.5. Sorting Rows (ORDER BY)">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="queries.html" title="Chapter 7. Queries">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="queries-values.html" title="7.7. VALUES Lists">Next</a></td></tr><tr><td width="40%" align="left" valign="top">7.5. Sorting Rows (<code class="literal">ORDER BY</code></td><td width="20%" align="center"><a accesskey="h" href="index.html" title="PostgreSQL 15.4 Documentation">Home</a></td><td width="40%" align="right" valign="top"> 7.7. <code class="literal">VALUES</code> Lists</td></tr></table></div></body></html>