codekingpro/portable-devtools
115k
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 16.3 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 just4 a portion of the rows that are generated by the rest of the query:5</p><pre class="synopsis">6SELECT <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>number</code></em> | ALL } </span>] [<span class="optional"> OFFSET <em class="replaceable"><code>number</code></em> </span>]10</pre><p>11 </p><p>12 If a limit count is given, no more than that many rows will be13 returned (but possibly fewer, if the query itself yields fewer rows).14 <code class="literal">LIMIT ALL</code> is the same as omitting the <code class="literal">LIMIT</code>15 clause, as is <code class="literal">LIMIT</code> with a NULL argument.16 </p><p>17 <code class="literal">OFFSET</code> says to skip that many rows before beginning to18 return rows. <code class="literal">OFFSET 0</code> is the same as omitting the19 <code class="literal">OFFSET</code> clause, as is <code class="literal">OFFSET</code> with a NULL argument.20 </p><p>21 If both <code class="literal">OFFSET</code>22 and <code class="literal">LIMIT</code> appear, then <code class="literal">OFFSET</code> rows are23 skipped before starting to count the <code class="literal">LIMIT</code> rows that24 are returned.25 </p><p>26 When using <code class="literal">LIMIT</code>, it is important to use an27 <code class="literal">ORDER BY</code> clause that constrains the result rows into a28 unique order. Otherwise you will get an unpredictable subset of29 the query's rows. You might be asking for the tenth through30 twentieth rows, but tenth through twentieth in what ordering? The31 ordering is unknown, unless you specified <code class="literal">ORDER BY</code>.32 </p><p>33 The query optimizer takes <code class="literal">LIMIT</code> into account when34 generating query plans, so you are very likely to get different35 plans (yielding different row orders) depending on what you give36 for <code class="literal">LIMIT</code> and <code class="literal">OFFSET</code>. Thus, using37 different <code class="literal">LIMIT</code>/<code class="literal">OFFSET</code> values to select38 different subsets of a query result <span class="emphasis"><em>will give39 inconsistent results</em></span> unless you enforce a predictable40 result ordering with <code class="literal">ORDER BY</code>. This is not a bug; it41 is an inherent consequence of the fact that SQL does not promise to42 deliver the results of a query in any particular order unless43 <code class="literal">ORDER BY</code> is used to constrain the order.44 </p><p>45 The rows skipped by an <code class="literal">OFFSET</code> clause still have to be46 computed inside the server; therefore a large <code class="literal">OFFSET</code>47 might be inefficient.48 </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 16.3 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>