1 <?xml version="1.0" encoding="UTF-8" standalone="no"?>
2 <!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 18.0 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> <a href="#QUERIES-LIMIT" class="id_link">#</a></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>
3 <code class="literal">LIMIT</code> and <code class="literal">OFFSET</code> allow you to retrieve just
4 a portion of the rows that are generated by the rest of the query:
5 </p><pre class="synopsis">
6 SELECT <em class="replaceable"><code>select_list</code></em>
7 FROM <em class="replaceable"><code>table_expression</code></em>
8 [<span class="optional"> ORDER BY ... </span>]
9 [<span class="optional"> LIMIT { <em class="replaceable"><code>count</code></em> | ALL } </span>]
10 [<span class="optional"> OFFSET <em class="replaceable"><code>start</code></em> </span>]
13 If a limit count is given, no more than that many rows will be
14 returned (but possibly fewer, if the query itself yields fewer rows).
15 <code class="literal">LIMIT ALL</code> is the same as omitting the <code class="literal">LIMIT</code>
16 clause, as is <code class="literal">LIMIT</code> with a NULL argument.
18 <code class="literal">OFFSET</code> says to skip that many rows before beginning to
19 return rows. <code class="literal">OFFSET 0</code> is the same as omitting the
20 <code class="literal">OFFSET</code> clause, as is <code class="literal">OFFSET</code> with a NULL argument.
22 If both <code class="literal">OFFSET</code>
23 and <code class="literal">LIMIT</code> appear, then <code class="literal">OFFSET</code> rows are
24 skipped before starting to count the <code class="literal">LIMIT</code> rows that
27 When using <code class="literal">LIMIT</code>, it is important to use an
28 <code class="literal">ORDER BY</code> clause that constrains the result rows into a
29 unique order. Otherwise you will get an unpredictable subset of
30 the query's rows. You might be asking for the tenth through
31 twentieth rows, but tenth through twentieth in what ordering? The
32 ordering is unknown, unless you specified <code class="literal">ORDER BY</code>.
34 The query optimizer takes <code class="literal">LIMIT</code> into account when
35 generating query plans, so you are very likely to get different
36 plans (yielding different row orders) depending on what you give
37 for <code class="literal">LIMIT</code> and <code class="literal">OFFSET</code>. Thus, using
38 different <code class="literal">LIMIT</code>/<code class="literal">OFFSET</code> values to select
39 different subsets of a query result <span class="emphasis"><em>will give
40 inconsistent results</em></span> unless you enforce a predictable
41 result ordering with <code class="literal">ORDER BY</code>. This is not a bug; it
42 is an inherent consequence of the fact that SQL does not promise to
43 deliver the results of a query in any particular order unless
44 <code class="literal">ORDER BY</code> is used to constrain the order.
46 The rows skipped by an <code class="literal">OFFSET</code> clause still have to be
47 computed inside the server; therefore a large <code class="literal">OFFSET</code>
49 </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 18.0 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>