gooderp18绿色标准版
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

1556 行
102KB

  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>SELECT</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 V1.79.1" /><link rel="prev" href="sql-security-label.html" title="SECURITY LABEL" /><link rel="next" href="sql-selectinto.html" title="SELECT INTO" /></head><body><div xmlns="http://www.w3.org/TR/xhtml1/transitional" class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="5" align="center">SELECT</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="sql-security-label.html" title="SECURITY LABEL">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="sql-commands.html" title="SQL Commands">Up</a></td><th width="60%" align="center">SQL Commands</th><td width="10%" align="right"><a accesskey="h" href="index.html" title="PostgreSQL 12.4 Documentation">Home</a></td><td width="10%" align="right"> <a accesskey="n" href="sql-selectinto.html" title="SELECT INTO">Next</a></td></tr></table><hr></hr></div><div class="refentry" id="SQL-SELECT"><div class="titlepage"></div><a id="id-1.9.3.171.1" class="indexterm"></a><a id="id-1.9.3.171.2" class="indexterm"></a><a id="id-1.9.3.171.3" class="indexterm"></a><div class="refnamediv"><h2><span class="refentrytitle">SELECT</span></h2><p>SELECT, TABLE, WITH — retrieve rows from a table or view</p></div><div class="refsynopsisdiv"><h2>Synopsis</h2><pre class="synopsis">
  3. [ WITH [ RECURSIVE ] <em class="replaceable"><code>with_query</code></em> [, ...] ]
  4. SELECT [ ALL | DISTINCT [ ON ( <em class="replaceable"><code>expression</code></em> [, ...] ) ] ]
  5. [ * | <em class="replaceable"><code>expression</code></em> [ [ AS ] <em class="replaceable"><code>output_name</code></em> ] [, ...] ]
  6. [ FROM <em class="replaceable"><code>from_item</code></em> [, ...] ]
  7. [ WHERE <em class="replaceable"><code>condition</code></em> ]
  8. [ GROUP BY <em class="replaceable"><code>grouping_element</code></em> [, ...] ]
  9. [ HAVING <em class="replaceable"><code>condition</code></em> [, ...] ]
  10. [ WINDOW <em class="replaceable"><code>window_name</code></em> AS ( <em class="replaceable"><code>window_definition</code></em> ) [, ...] ]
  11. [ { UNION | INTERSECT | EXCEPT } [ ALL | DISTINCT ] <em class="replaceable"><code>select</code></em> ]
  12. [ ORDER BY <em class="replaceable"><code>expression</code></em> [ ASC | DESC | USING <em class="replaceable"><code>operator</code></em> ] [ NULLS { FIRST | LAST } ] [, ...] ]
  13. [ LIMIT { <em class="replaceable"><code>count</code></em> | ALL } ]
  14. [ OFFSET <em class="replaceable"><code>start</code></em> [ ROW | ROWS ] ]
  15. [ FETCH { FIRST | NEXT } [ <em class="replaceable"><code>count</code></em> ] { ROW | ROWS } ONLY ]
  16. [ FOR { UPDATE | NO KEY UPDATE | SHARE | KEY SHARE } [ OF <em class="replaceable"><code>table_name</code></em> [, ...] ] [ NOWAIT | SKIP LOCKED ] [...] ]
  17. <span class="phrase">where <em class="replaceable"><code>from_item</code></em> can be one of:</span>
  18. [ ONLY ] <em class="replaceable"><code>table_name</code></em> [ * ] [ [ AS ] <em class="replaceable"><code>alias</code></em> [ ( <em class="replaceable"><code>column_alias</code></em> [, ...] ) ] ]
  19. [ TABLESAMPLE <em class="replaceable"><code>sampling_method</code></em> ( <em class="replaceable"><code>argument</code></em> [, ...] ) [ REPEATABLE ( <em class="replaceable"><code>seed</code></em> ) ] ]
  20. [ LATERAL ] ( <em class="replaceable"><code>select</code></em> ) [ AS ] <em class="replaceable"><code>alias</code></em> [ ( <em class="replaceable"><code>column_alias</code></em> [, ...] ) ]
  21. <em class="replaceable"><code>with_query_name</code></em> [ [ AS ] <em class="replaceable"><code>alias</code></em> [ ( <em class="replaceable"><code>column_alias</code></em> [, ...] ) ] ]
  22. [ LATERAL ] <em class="replaceable"><code>function_name</code></em> ( [ <em class="replaceable"><code>argument</code></em> [, ...] ] )
  23. [ WITH ORDINALITY ] [ [ AS ] <em class="replaceable"><code>alias</code></em> [ ( <em class="replaceable"><code>column_alias</code></em> [, ...] ) ] ]
  24. [ LATERAL ] <em class="replaceable"><code>function_name</code></em> ( [ <em class="replaceable"><code>argument</code></em> [, ...] ] ) [ AS ] <em class="replaceable"><code>alias</code></em> ( <em class="replaceable"><code>column_definition</code></em> [, ...] )
  25. [ LATERAL ] <em class="replaceable"><code>function_name</code></em> ( [ <em class="replaceable"><code>argument</code></em> [, ...] ] ) AS ( <em class="replaceable"><code>column_definition</code></em> [, ...] )
  26. [ LATERAL ] ROWS FROM( <em class="replaceable"><code>function_name</code></em> ( [ <em class="replaceable"><code>argument</code></em> [, ...] ] ) [ AS ( <em class="replaceable"><code>column_definition</code></em> [, ...] ) ] [, ...] )
  27. [ WITH ORDINALITY ] [ [ AS ] <em class="replaceable"><code>alias</code></em> [ ( <em class="replaceable"><code>column_alias</code></em> [, ...] ) ] ]
  28. <em class="replaceable"><code>from_item</code></em> [ NATURAL ] <em class="replaceable"><code>join_type</code></em> <em class="replaceable"><code>from_item</code></em> [ ON <em class="replaceable"><code>join_condition</code></em> | USING ( <em class="replaceable"><code>join_column</code></em> [, ...] ) ]
  29. <span class="phrase">and <em class="replaceable"><code>grouping_element</code></em> can be one of:</span>
  30. ( )
  31. <em class="replaceable"><code>expression</code></em>
  32. ( <em class="replaceable"><code>expression</code></em> [, ...] )
  33. ROLLUP ( { <em class="replaceable"><code>expression</code></em> | ( <em class="replaceable"><code>expression</code></em> [, ...] ) } [, ...] )
  34. CUBE ( { <em class="replaceable"><code>expression</code></em> | ( <em class="replaceable"><code>expression</code></em> [, ...] ) } [, ...] )
  35. GROUPING SETS ( <em class="replaceable"><code>grouping_element</code></em> [, ...] )
  36. <span class="phrase">and <em class="replaceable"><code>with_query</code></em> is:</span>
  37. <em class="replaceable"><code>with_query_name</code></em> [ ( <em class="replaceable"><code>column_name</code></em> [, ...] ) ] AS [ [ NOT ] MATERIALIZED ] ( <em class="replaceable"><code>select</code></em> | <em class="replaceable"><code>values</code></em> | <em class="replaceable"><code>insert</code></em> | <em class="replaceable"><code>update</code></em> | <em class="replaceable"><code>delete</code></em> )
  38. TABLE [ ONLY ] <em class="replaceable"><code>table_name</code></em> [ * ]
  39. </pre></div><div class="refsect1" id="id-1.9.3.171.7"><h2>Description</h2><p>
  40. <code class="command">SELECT</code> retrieves rows from zero or more tables.
  41. The general processing of <code class="command">SELECT</code> is as follows:
  42. </p><div class="orderedlist"><ol class="orderedlist" type="1"><li class="listitem"><p>
  43. All queries in the <code class="literal">WITH</code> list are computed.
  44. These effectively serve as temporary tables that can be referenced
  45. in the <code class="literal">FROM</code> list. A <code class="literal">WITH</code> query
  46. that is referenced more than once in <code class="literal">FROM</code> is
  47. computed only once,
  48. unless specified otherwise with <code class="literal">NOT MATERIALIZED</code>.
  49. (See <a class="xref" href="sql-select.html#SQL-WITH" title="WITH Clause"><code class="literal">WITH</code> Clause</a> below.)
  50. </p></li><li class="listitem"><p>
  51. All elements in the <code class="literal">FROM</code> list are computed.
  52. (Each element in the <code class="literal">FROM</code> list is a real or
  53. virtual table.) If more than one element is specified in the
  54. <code class="literal">FROM</code> list, they are cross-joined together.
  55. (See <a class="xref" href="sql-select.html#SQL-FROM" title="FROM Clause"><code class="literal">FROM</code> Clause</a> below.)
  56. </p></li><li class="listitem"><p>
  57. If the <code class="literal">WHERE</code> clause is specified, all rows
  58. that do not satisfy the condition are eliminated from the
  59. output. (See <a class="xref" href="sql-select.html#SQL-WHERE" title="WHERE Clause"><code class="literal">WHERE</code> Clause</a> below.)
  60. </p></li><li class="listitem"><p>
  61. If the <code class="literal">GROUP BY</code> clause is specified,
  62. or if there are aggregate function calls, the
  63. output is combined into groups of rows that match on one or more
  64. values, and the results of aggregate functions are computed.
  65. If the <code class="literal">HAVING</code> clause is present, it
  66. eliminates groups that do not satisfy the given condition. (See
  67. <a class="xref" href="sql-select.html#SQL-GROUPBY" title="GROUP BY Clause"><code class="literal">GROUP BY</code> Clause</a> and
  68. <a class="xref" href="sql-select.html#SQL-HAVING" title="HAVING Clause"><code class="literal">HAVING</code> Clause</a> below.)
  69. </p></li><li class="listitem"><p>
  70. The actual output rows are computed using the
  71. <code class="command">SELECT</code> output expressions for each selected
  72. row or row group. (See
  73. <a class="xref" href="sql-select.html#SQL-SELECT-LIST" title="SELECT List"><code class="command">SELECT</code> List</a>
  74. below.)
  75. </p></li><li class="listitem"><p><code class="literal">SELECT DISTINCT</code> eliminates duplicate rows from the
  76. result. <code class="literal">SELECT DISTINCT ON</code> eliminates rows that
  77. match on all the specified expressions. <code class="literal">SELECT ALL</code>
  78. (the default) will return all candidate rows, including
  79. duplicates. (See <a class="xref" href="sql-select.html#SQL-DISTINCT" title="DISTINCT Clause"><code class="literal">DISTINCT</code> Clause</a> below.)
  80. </p></li><li class="listitem"><p>
  81. Using the operators <code class="literal">UNION</code>,
  82. <code class="literal">INTERSECT</code>, and <code class="literal">EXCEPT</code>, the
  83. output of more than one <code class="command">SELECT</code> statement can
  84. be combined to form a single result set. The
  85. <code class="literal">UNION</code> operator returns all rows that are in
  86. one or both of the result sets. The
  87. <code class="literal">INTERSECT</code> operator returns all rows that are
  88. strictly in both result sets. The <code class="literal">EXCEPT</code>
  89. operator returns the rows that are in the first result set but
  90. not in the second. In all three cases, duplicate rows are
  91. eliminated unless <code class="literal">ALL</code> is specified. The noise
  92. word <code class="literal">DISTINCT</code> can be added to explicitly specify
  93. eliminating duplicate rows. Notice that <code class="literal">DISTINCT</code> is
  94. the default behavior here, even though <code class="literal">ALL</code> is
  95. the default for <code class="command">SELECT</code> itself. (See
  96. <a class="xref" href="sql-select.html#SQL-UNION" title="UNION Clause"><code class="literal">UNION</code> Clause</a>, <a class="xref" href="sql-select.html#SQL-INTERSECT" title="INTERSECT Clause"><code class="literal">INTERSECT</code> Clause</a>, and
  97. <a class="xref" href="sql-select.html#SQL-EXCEPT" title="EXCEPT Clause"><code class="literal">EXCEPT</code> Clause</a> below.)
  98. </p></li><li class="listitem"><p>
  99. If the <code class="literal">ORDER BY</code> clause is specified, the
  100. returned rows are sorted in the specified order. If
  101. <code class="literal">ORDER BY</code> is not given, the rows are returned
  102. in whatever order the system finds fastest to produce. (See
  103. <a class="xref" href="sql-select.html#SQL-ORDERBY" title="ORDER BY Clause"><code class="literal">ORDER BY</code> Clause</a> below.)
  104. </p></li><li class="listitem"><p>
  105. If the <code class="literal">LIMIT</code> (or <code class="literal">FETCH FIRST</code>) or <code class="literal">OFFSET</code>
  106. clause is specified, the <code class="command">SELECT</code> statement
  107. only returns a subset of the result rows. (See <a class="xref" href="sql-select.html#SQL-LIMIT" title="LIMIT Clause"><code class="literal">LIMIT</code> Clause</a> below.)
  108. </p></li><li class="listitem"><p>
  109. If <code class="literal">FOR UPDATE</code>, <code class="literal">FOR NO KEY UPDATE</code>, <code class="literal">FOR SHARE</code>
  110. or <code class="literal">FOR KEY SHARE</code>
  111. is specified, the
  112. <code class="command">SELECT</code> statement locks the selected rows
  113. against concurrent updates. (See <a class="xref" href="sql-select.html#SQL-FOR-UPDATE-SHARE" title="The Locking Clause">The Locking Clause</a> below.)
  114. </p></li></ol></div><p>
  115. </p><p>
  116. You must have <code class="literal">SELECT</code> privilege on each column used
  117. in a <code class="command">SELECT</code> command. The use of <code class="literal">FOR NO KEY UPDATE</code>,
  118. <code class="literal">FOR UPDATE</code>,
  119. <code class="literal">FOR SHARE</code> or <code class="literal">FOR KEY SHARE</code> requires
  120. <code class="literal">UPDATE</code> privilege as well (for at least one column
  121. of each table so selected).
  122. </p></div><div class="refsect1" id="id-1.9.3.171.8"><h2>Parameters</h2><div class="refsect2" id="SQL-WITH"><h3><code class="literal">WITH</code> Clause</h3><p>
  123. The <code class="literal">WITH</code> clause allows you to specify one or more
  124. subqueries that can be referenced by name in the primary query.
  125. The subqueries effectively act as temporary tables or views
  126. for the duration of the primary query.
  127. Each subquery can be a <code class="command">SELECT</code>, <code class="command">TABLE</code>, <code class="command">VALUES</code>,
  128. <code class="command">INSERT</code>, <code class="command">UPDATE</code> or
  129. <code class="command">DELETE</code> statement.
  130. When writing a data-modifying statement (<code class="command">INSERT</code>,
  131. <code class="command">UPDATE</code> or <code class="command">DELETE</code>) in
  132. <code class="literal">WITH</code>, it is usual to include a <code class="literal">RETURNING</code> clause.
  133. It is the output of <code class="literal">RETURNING</code>, <span class="emphasis"><em>not</em></span> the underlying
  134. table that the statement modifies, that forms the temporary table that is
  135. read by the primary query. If <code class="literal">RETURNING</code> is omitted, the
  136. statement is still executed, but it produces no output so it cannot be
  137. referenced as a table by the primary query.
  138. </p><p>
  139. A name (without schema qualification) must be specified for each
  140. <code class="literal">WITH</code> query. Optionally, a list of column names
  141. can be specified; if this is omitted,
  142. the column names are inferred from the subquery.
  143. </p><p>
  144. If <code class="literal">RECURSIVE</code> is specified, it allows a
  145. <code class="command">SELECT</code> subquery to reference itself by name. Such a
  146. subquery must have the form
  147. </p><pre class="synopsis">
  148. <em class="replaceable"><code>non_recursive_term</code></em> UNION [ ALL | DISTINCT ] <em class="replaceable"><code>recursive_term</code></em>
  149. </pre><p>
  150. where the recursive self-reference must appear on the right-hand
  151. side of the <code class="literal">UNION</code>. Only one recursive self-reference
  152. is permitted per query. Recursive data-modifying statements are not
  153. supported, but you can use the results of a recursive
  154. <code class="command">SELECT</code> query in
  155. a data-modifying statement. See <a class="xref" href="queries-with.html" title="7.8. WITH Queries (Common Table Expressions)">Section 7.8</a> for
  156. an example.
  157. </p><p>
  158. Another effect of <code class="literal">RECURSIVE</code> is that
  159. <code class="literal">WITH</code> queries need not be ordered: a query
  160. can reference another one that is later in the list. (However,
  161. circular references, or mutual recursion, are not implemented.)
  162. Without <code class="literal">RECURSIVE</code>, <code class="literal">WITH</code> queries
  163. can only reference sibling <code class="literal">WITH</code> queries
  164. that are earlier in the <code class="literal">WITH</code> list.
  165. </p><p>
  166. When there are multiple queries in the <code class="literal">WITH</code>
  167. clause, <code class="literal">RECURSIVE</code> should be written only once,
  168. immediately after <code class="literal">WITH</code>. It applies to all queries
  169. in the <code class="literal">WITH</code> clause, though it has no effect on
  170. queries that do not use recursion or forward references.
  171. </p><p>
  172. The primary query and the <code class="literal">WITH</code> queries are all
  173. (notionally) executed at the same time. This implies that the effects of
  174. a data-modifying statement in <code class="literal">WITH</code> cannot be seen from
  175. other parts of the query, other than by reading its <code class="literal">RETURNING</code>
  176. output. If two such data-modifying statements attempt to modify the same
  177. row, the results are unspecified.
  178. </p><p>
  179. A key property of <code class="literal">WITH</code> queries is that they
  180. are normally evaluated only once per execution of the primary query,
  181. even if the primary query refers to them more than once.
  182. In particular, data-modifying statements are guaranteed to be
  183. executed once and only once, regardless of whether the primary query
  184. reads all or any of their output.
  185. </p><p>
  186. However, a <code class="literal">WITH</code> query can be marked
  187. <code class="literal">NOT MATERIALIZED</code> to remove this guarantee. In that
  188. case, the <code class="literal">WITH</code> query can be folded into the primary
  189. query much as though it were a simple sub-<code class="literal">SELECT</code> in
  190. the primary query's <code class="literal">FROM</code> clause. This results in
  191. duplicate computations if the primary query refers to
  192. that <code class="literal">WITH</code> query more than once; but if each such use
  193. requires only a few rows of the <code class="literal">WITH</code> query's total
  194. output, <code class="literal">NOT MATERIALIZED</code> can provide a net savings by
  195. allowing the queries to be optimized jointly.
  196. <code class="literal">NOT MATERIALIZED</code> is ignored if it is attached to
  197. a <code class="literal">WITH</code> query that is recursive or is not
  198. side-effect-free (i.e., is not a plain <code class="literal">SELECT</code>
  199. containing no volatile functions).
  200. </p><p>
  201. By default, a side-effect-free <code class="literal">WITH</code> query is folded
  202. into the primary query if it is used exactly once in the primary
  203. query's <code class="literal">FROM</code> clause. This allows joint optimization
  204. of the two query levels in situations where that should be semantically
  205. invisible. However, such folding can be prevented by marking the
  206. <code class="literal">WITH</code> query as <code class="literal">MATERIALIZED</code>.
  207. That might be useful, for example, if the <code class="literal">WITH</code> query
  208. is being used as an optimization fence to prevent the planner from
  209. choosing a bad plan.
  210. <span class="productname">PostgreSQL</span> versions before v12 never did
  211. such folding, so queries written for older versions might rely on
  212. <code class="literal">WITH</code> to act as an optimization fence.
  213. </p><p>
  214. See <a class="xref" href="queries-with.html" title="7.8. WITH Queries (Common Table Expressions)">Section 7.8</a> for additional information.
  215. </p></div><div class="refsect2" id="SQL-FROM"><h3><code class="literal">FROM</code> Clause</h3><p>
  216. The <code class="literal">FROM</code> clause specifies one or more source
  217. tables for the <code class="command">SELECT</code>. If multiple sources are
  218. specified, the result is the Cartesian product (cross join) of all
  219. the sources. But usually qualification conditions are added (via
  220. <code class="literal">WHERE</code>) to restrict the returned rows to a small subset of the
  221. Cartesian product.
  222. </p><p>
  223. The <code class="literal">FROM</code> clause can contain the following
  224. elements:
  225. </p><div class="variablelist"><dl class="variablelist"><dt><span class="term"><em class="replaceable"><code>table_name</code></em></span></dt><dd><p>
  226. The name (optionally schema-qualified) of an existing table or view.
  227. If <code class="literal">ONLY</code> is specified before the table name, only that
  228. table is scanned. If <code class="literal">ONLY</code> is not specified, the table
  229. and all its descendant tables (if any) are scanned. Optionally,
  230. <code class="literal">*</code> can be specified after the table name to explicitly
  231. indicate that descendant tables are included.
  232. </p></dd><dt><span class="term"><em class="replaceable"><code>alias</code></em></span></dt><dd><p>
  233. A substitute name for the <code class="literal">FROM</code> item containing the
  234. alias. An alias is used for brevity or to eliminate ambiguity
  235. for self-joins (where the same table is scanned multiple
  236. times). When an alias is provided, it completely hides the
  237. actual name of the table or function; for example given
  238. <code class="literal">FROM foo AS f</code>, the remainder of the
  239. <code class="command">SELECT</code> must refer to this <code class="literal">FROM</code>
  240. item as <code class="literal">f</code> not <code class="literal">foo</code>. If an alias is
  241. written, a column alias list can also be written to provide
  242. substitute names for one or more columns of the table.
  243. </p></dd><dt><span class="term"><code class="literal">TABLESAMPLE <em class="replaceable"><code>sampling_method</code></em> ( <em class="replaceable"><code>argument</code></em> [, ...] ) [ REPEATABLE ( <em class="replaceable"><code>seed</code></em> ) ]</code></span></dt><dd><p>
  244. A <code class="literal">TABLESAMPLE</code> clause after
  245. a <em class="replaceable"><code>table_name</code></em> indicates that the
  246. specified <em class="replaceable"><code>sampling_method</code></em>
  247. should be used to retrieve a subset of the rows in that table.
  248. This sampling precedes the application of any other filters such
  249. as <code class="literal">WHERE</code> clauses.
  250. The standard <span class="productname">PostgreSQL</span> distribution
  251. includes two sampling methods, <code class="literal">BERNOULLI</code>
  252. and <code class="literal">SYSTEM</code>, and other sampling methods can be
  253. installed in the database via extensions.
  254. </p><p>
  255. The <code class="literal">BERNOULLI</code> and <code class="literal">SYSTEM</code> sampling methods
  256. each accept a single <em class="replaceable"><code>argument</code></em>
  257. which is the fraction of the table to sample, expressed as a
  258. percentage between 0 and 100. This argument can be
  259. any <code class="type">real</code>-valued expression. (Other sampling methods might
  260. accept more or different arguments.) These two methods each return
  261. a randomly-chosen sample of the table that will contain
  262. approximately the specified percentage of the table's rows.
  263. The <code class="literal">BERNOULLI</code> method scans the whole table and
  264. selects or ignores individual rows independently with the specified
  265. probability.
  266. The <code class="literal">SYSTEM</code> method does block-level sampling with
  267. each block having the specified chance of being selected; all rows
  268. in each selected block are returned.
  269. The <code class="literal">SYSTEM</code> method is significantly faster than
  270. the <code class="literal">BERNOULLI</code> method when small sampling
  271. percentages are specified, but it may return a less-random sample of
  272. the table as a result of clustering effects.
  273. </p><p>
  274. The optional <code class="literal">REPEATABLE</code> clause specifies
  275. a <em class="replaceable"><code>seed</code></em> number or expression to use
  276. for generating random numbers within the sampling method. The seed
  277. value can be any non-null floating-point value. Two queries that
  278. specify the same seed and <em class="replaceable"><code>argument</code></em>
  279. values will select the same sample of the table, if the table has
  280. not been changed meanwhile. But different seed values will usually
  281. produce different samples.
  282. If <code class="literal">REPEATABLE</code> is not given then a new random
  283. sample is selected for each query, based upon a system-generated seed.
  284. Note that some add-on sampling methods do not
  285. accept <code class="literal">REPEATABLE</code>, and will always produce new
  286. samples on each use.
  287. </p></dd><dt><span class="term"><em class="replaceable"><code>select</code></em></span></dt><dd><p>
  288. A sub-<code class="command">SELECT</code> can appear in the
  289. <code class="literal">FROM</code> clause. This acts as though its
  290. output were created as a temporary table for the duration of
  291. this single <code class="command">SELECT</code> command. Note that the
  292. sub-<code class="command">SELECT</code> must be surrounded by
  293. parentheses, and an alias <span class="emphasis"><em>must</em></span> be
  294. provided for it. A
  295. <a class="xref" href="sql-values.html" title="VALUES"><span class="refentrytitle">VALUES</span></a> command
  296. can also be used here.
  297. </p></dd><dt><span class="term"><em class="replaceable"><code>with_query_name</code></em></span></dt><dd><p>
  298. A <code class="literal">WITH</code> query is referenced by writing its name,
  299. just as though the query's name were a table name. (In fact,
  300. the <code class="literal">WITH</code> query hides any real table of the same name
  301. for the purposes of the primary query. If necessary, you can
  302. refer to a real table of the same name by schema-qualifying
  303. the table's name.)
  304. An alias can be provided in the same way as for a table.
  305. </p></dd><dt><span class="term"><em class="replaceable"><code>function_name</code></em></span></dt><dd><p>
  306. Function calls can appear in the <code class="literal">FROM</code>
  307. clause. (This is especially useful for functions that return
  308. result sets, but any function can be used.) This acts as
  309. though the function's output were created as a temporary table for the
  310. duration of this single <code class="command">SELECT</code> command.
  311. When the optional <code class="command">WITH ORDINALITY</code> clause is
  312. added to the function call, a new column is appended after
  313. all the function's output columns with numbering for each row.
  314. </p><p>
  315. An alias can be provided in the same way as for a table.
  316. If an alias is written, a column
  317. alias list can also be written to provide substitute names for
  318. one or more attributes of the function's composite return
  319. type, including the column added by <code class="literal">ORDINALITY</code>
  320. if present.
  321. </p><p>
  322. Multiple function calls can be combined into a
  323. single <code class="literal">FROM</code>-clause item by surrounding them
  324. with <code class="literal">ROWS FROM( ... )</code>. The output of such an item is the
  325. concatenation of the first row from each function, then the second
  326. row from each function, etc. If some of the functions produce fewer
  327. rows than others, null values are substituted for the missing data, so
  328. that the total number of rows returned is always the same as for the
  329. function that produced the most rows.
  330. </p><p>
  331. If the function has been defined as returning the
  332. <code class="type">record</code> data type, then an alias or the key word
  333. <code class="literal">AS</code> must be present, followed by a column
  334. definition list in the form <code class="literal">( <em class="replaceable"><code>column_name</code></em> <em class="replaceable"><code>data_type</code></em> [<span class="optional">, ...
  335. </span>])</code>. The column definition list must match the
  336. actual number and types of columns returned by the function.
  337. </p><p>
  338. When using the <code class="literal">ROWS FROM( ... )</code> syntax, if one of the
  339. functions requires a column definition list, it's preferred to put
  340. the column definition list after the function call inside
  341. <code class="literal">ROWS FROM( ... )</code>. A column definition list can be placed
  342. after the <code class="literal">ROWS FROM( ... )</code> construct only if there's just
  343. a single function and no <code class="literal">WITH ORDINALITY</code> clause.
  344. </p><p>
  345. To use <code class="literal">ORDINALITY</code> together with a column definition
  346. list, you must use the <code class="literal">ROWS FROM( ... )</code> syntax and put the
  347. column definition list inside <code class="literal">ROWS FROM( ... )</code>.
  348. </p></dd><dt><span class="term"><em class="replaceable"><code>join_type</code></em></span></dt><dd><p>
  349. One of
  350. </p><div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p><code class="literal">[ INNER ] JOIN</code></p></li><li class="listitem"><p><code class="literal">LEFT [ OUTER ] JOIN</code></p></li><li class="listitem"><p><code class="literal">RIGHT [ OUTER ] JOIN</code></p></li><li class="listitem"><p><code class="literal">FULL [ OUTER ] JOIN</code></p></li><li class="listitem"><p><code class="literal">CROSS JOIN</code></p></li></ul></div><p>
  351. For the <code class="literal">INNER</code> and <code class="literal">OUTER</code> join types, a
  352. join condition must be specified, namely exactly one of
  353. <code class="literal">NATURAL</code>, <code class="literal">ON <em class="replaceable"><code>join_condition</code></em></code>, or
  354. <code class="literal">USING (<em class="replaceable"><code>join_column</code></em> [, ...])</code>.
  355. See below for the meaning. For <code class="literal">CROSS JOIN</code>,
  356. none of these clauses can appear.
  357. </p><p>
  358. A <code class="literal">JOIN</code> clause combines two <code class="literal">FROM</code>
  359. items, which for convenience we will refer to as <span class="quote">“<span class="quote">tables</span>”</span>,
  360. though in reality they can be any type of <code class="literal">FROM</code> item.
  361. Use parentheses if necessary to determine the order of nesting.
  362. In the absence of parentheses, <code class="literal">JOIN</code>s nest
  363. left-to-right. In any case <code class="literal">JOIN</code> binds more
  364. tightly than the commas separating <code class="literal">FROM</code>-list items.
  365. </p><p><code class="literal">CROSS JOIN</code> and <code class="literal">INNER JOIN</code>
  366. produce a simple Cartesian product, the same result as you get from
  367. listing the two tables at the top level of <code class="literal">FROM</code>,
  368. but restricted by the join condition (if any).
  369. <code class="literal">CROSS JOIN</code> is equivalent to <code class="literal">INNER JOIN ON
  370. (TRUE)</code>, that is, no rows are removed by qualification.
  371. These join types are just a notational convenience, since they
  372. do nothing you couldn't do with plain <code class="literal">FROM</code> and
  373. <code class="literal">WHERE</code>.
  374. </p><p><code class="literal">LEFT OUTER JOIN</code> returns all rows in the qualified
  375. Cartesian product (i.e., all combined rows that pass its join
  376. condition), plus one copy of each row in the left-hand table
  377. for which there was no right-hand row that passed the join
  378. condition. This left-hand row is extended to the full width
  379. of the joined table by inserting null values for the
  380. right-hand columns. Note that only the <code class="literal">JOIN</code>
  381. clause's own condition is considered while deciding which rows
  382. have matches. Outer conditions are applied afterwards.
  383. </p><p>
  384. Conversely, <code class="literal">RIGHT OUTER JOIN</code> returns all the
  385. joined rows, plus one row for each unmatched right-hand row
  386. (extended with nulls on the left). This is just a notational
  387. convenience, since you could convert it to a <code class="literal">LEFT
  388. OUTER JOIN</code> by switching the left and right tables.
  389. </p><p><code class="literal">FULL OUTER JOIN</code> returns all the joined rows, plus
  390. one row for each unmatched left-hand row (extended with nulls
  391. on the right), plus one row for each unmatched right-hand row
  392. (extended with nulls on the left).
  393. </p></dd><dt><span class="term"><code class="literal">ON <em class="replaceable"><code>join_condition</code></em></code></span></dt><dd><p><em class="replaceable"><code>join_condition</code></em> is
  394. an expression resulting in a value of type
  395. <code class="type">boolean</code> (similar to a <code class="literal">WHERE</code>
  396. clause) that specifies which rows in a join are considered to
  397. match.
  398. </p></dd><dt><span class="term"><code class="literal">USING ( <em class="replaceable"><code>join_column</code></em> [, ...] )</code></span></dt><dd><p>
  399. A clause of the form <code class="literal">USING ( a, b, ... )</code> is
  400. shorthand for <code class="literal">ON left_table.a = right_table.a AND
  401. left_table.b = right_table.b ...</code>. Also,
  402. <code class="literal">USING</code> implies that only one of each pair of
  403. equivalent columns will be included in the join output, not
  404. both.
  405. </p></dd><dt><span class="term"><code class="literal">NATURAL</code></span></dt><dd><p>
  406. <code class="literal">NATURAL</code> is shorthand for a
  407. <code class="literal">USING</code> list that mentions all columns in the two
  408. tables that have matching names. If there are no common
  409. column names, <code class="literal">NATURAL</code> is equivalent
  410. to <code class="literal">ON TRUE</code>.
  411. </p></dd><dt><span class="term"><code class="literal">LATERAL</code></span></dt><dd><p>
  412. The <code class="literal">LATERAL</code> key word can precede a
  413. sub-<code class="command">SELECT</code> <code class="literal">FROM</code> item. This allows the
  414. sub-<code class="command">SELECT</code> to refer to columns of <code class="literal">FROM</code>
  415. items that appear before it in the <code class="literal">FROM</code> list. (Without
  416. <code class="literal">LATERAL</code>, each sub-<code class="command">SELECT</code> is
  417. evaluated independently and so cannot cross-reference any other
  418. <code class="literal">FROM</code> item.)
  419. </p><p><code class="literal">LATERAL</code> can also precede a function-call
  420. <code class="literal">FROM</code> item, but in this case it is a noise word, because
  421. the function expression can refer to earlier <code class="literal">FROM</code> items
  422. in any case.
  423. </p><p>
  424. A <code class="literal">LATERAL</code> item can appear at top level in the
  425. <code class="literal">FROM</code> list, or within a <code class="literal">JOIN</code> tree. In the
  426. latter case it can also refer to any items that are on the left-hand
  427. side of a <code class="literal">JOIN</code> that it is on the right-hand side of.
  428. </p><p>
  429. When a <code class="literal">FROM</code> item contains <code class="literal">LATERAL</code>
  430. cross-references, evaluation proceeds as follows: for each row of the
  431. <code class="literal">FROM</code> item providing the cross-referenced column(s), or
  432. set of rows of multiple <code class="literal">FROM</code> items providing the
  433. columns, the <code class="literal">LATERAL</code> item is evaluated using that
  434. row or row set's values of the columns. The resulting row(s) are
  435. joined as usual with the rows they were computed from. This is
  436. repeated for each row or set of rows from the column source table(s).
  437. </p><p>
  438. The column source table(s) must be <code class="literal">INNER</code> or
  439. <code class="literal">LEFT</code> joined to the <code class="literal">LATERAL</code> item, else
  440. there would not be a well-defined set of rows from which to compute
  441. each set of rows for the <code class="literal">LATERAL</code> item. Thus,
  442. although a construct such as <code class="literal"><em class="replaceable"><code>X</code></em> RIGHT JOIN
  443. LATERAL <em class="replaceable"><code>Y</code></em></code> is syntactically valid, it is
  444. not actually allowed for <em class="replaceable"><code>Y</code></em> to reference
  445. <em class="replaceable"><code>X</code></em>.
  446. </p></dd></dl></div><p>
  447. </p></div><div class="refsect2" id="SQL-WHERE"><h3><code class="literal">WHERE</code> Clause</h3><p>
  448. The optional <code class="literal">WHERE</code> clause has the general form
  449. </p><pre class="synopsis">
  450. WHERE <em class="replaceable"><code>condition</code></em>
  451. </pre><p>
  452. where <em class="replaceable"><code>condition</code></em> is
  453. any expression that evaluates to a result of type
  454. <code class="type">boolean</code>. Any row that does not satisfy this
  455. condition will be eliminated from the output. A row satisfies the
  456. condition if it returns true when the actual row values are
  457. substituted for any variable references.
  458. </p></div><div class="refsect2" id="SQL-GROUPBY"><h3><code class="literal">GROUP BY</code> Clause</h3><p>
  459. The optional <code class="literal">GROUP BY</code> clause has the general form
  460. </p><pre class="synopsis">
  461. GROUP BY <em class="replaceable"><code>grouping_element</code></em> [, ...]
  462. </pre><p>
  463. </p><p>
  464. <code class="literal">GROUP BY</code> will condense into a single row all
  465. selected rows that share the same values for the grouped
  466. expressions. An <em class="replaceable"><code>expression</code></em> used inside a
  467. <em class="replaceable"><code>grouping_element</code></em>
  468. can be an input column name, or the name or ordinal number of an
  469. output column (<code class="command">SELECT</code> list item), or an arbitrary
  470. expression formed from input-column values. In case of ambiguity,
  471. a <code class="literal">GROUP BY</code> name will be interpreted as an
  472. input-column name rather than an output column name.
  473. </p><p>
  474. If any of <code class="literal">GROUPING SETS</code>, <code class="literal">ROLLUP</code> or
  475. <code class="literal">CUBE</code> are present as grouping elements, then the
  476. <code class="literal">GROUP BY</code> clause as a whole defines some number of
  477. independent <em class="replaceable"><code>grouping sets</code></em>. The effect of this is
  478. equivalent to constructing a <code class="literal">UNION ALL</code> between
  479. subqueries with the individual grouping sets as their
  480. <code class="literal">GROUP BY</code> clauses. For further details on the handling
  481. of grouping sets see <a class="xref" href="queries-table-expressions.html#QUERIES-GROUPING-SETS" title="7.2.4. GROUPING SETS, CUBE, and ROLLUP">Section 7.2.4</a>.
  482. </p><p>
  483. Aggregate functions, if any are used, are computed across all rows
  484. making up each group, producing a separate value for each group.
  485. (If there are aggregate functions but no <code class="literal">GROUP BY</code>
  486. clause, the query is treated as having a single group comprising all
  487. the selected rows.)
  488. The set of rows fed to each aggregate function can be further filtered by
  489. attaching a <code class="literal">FILTER</code> clause to the aggregate function
  490. call; see <a class="xref" href="sql-expressions.html#SYNTAX-AGGREGATES" title="4.2.7. Aggregate Expressions">Section 4.2.7</a> for more information. When
  491. a <code class="literal">FILTER</code> clause is present, only those rows matching it
  492. are included in the input to that aggregate function.
  493. </p><p>
  494. When <code class="literal">GROUP BY</code> is present,
  495. or any aggregate functions are present, it is not valid for
  496. the <code class="command">SELECT</code> list expressions to refer to
  497. ungrouped columns except within aggregate functions or when the
  498. ungrouped column is functionally dependent on the grouped columns,
  499. since there would otherwise be more than one possible value to
  500. return for an ungrouped column. A functional dependency exists if
  501. the grouped columns (or a subset thereof) are the primary key of
  502. the table containing the ungrouped column.
  503. </p><p>
  504. Keep in mind that all aggregate functions are evaluated before
  505. evaluating any <span class="quote">“<span class="quote">scalar</span>”</span> expressions in the <code class="literal">HAVING</code>
  506. clause or <code class="literal">SELECT</code> list. This means that, for example,
  507. a <code class="literal">CASE</code> expression cannot be used to skip evaluation of
  508. an aggregate function; see <a class="xref" href="sql-expressions.html#SYNTAX-EXPRESS-EVAL" title="4.2.14. Expression Evaluation Rules">Section 4.2.14</a>.
  509. </p><p>
  510. Currently, <code class="literal">FOR NO KEY UPDATE</code>, <code class="literal">FOR UPDATE</code>,
  511. <code class="literal">FOR SHARE</code> and <code class="literal">FOR KEY SHARE</code> cannot be
  512. specified with <code class="literal">GROUP BY</code>.
  513. </p></div><div class="refsect2" id="SQL-HAVING"><h3><code class="literal">HAVING</code> Clause</h3><p>
  514. The optional <code class="literal">HAVING</code> clause has the general form
  515. </p><pre class="synopsis">
  516. HAVING <em class="replaceable"><code>condition</code></em>
  517. </pre><p>
  518. where <em class="replaceable"><code>condition</code></em> is
  519. the same as specified for the <code class="literal">WHERE</code> clause.
  520. </p><p>
  521. <code class="literal">HAVING</code> eliminates group rows that do not
  522. satisfy the condition. <code class="literal">HAVING</code> is different
  523. from <code class="literal">WHERE</code>: <code class="literal">WHERE</code> filters
  524. individual rows before the application of <code class="literal">GROUP
  525. BY</code>, while <code class="literal">HAVING</code> filters group rows
  526. created by <code class="literal">GROUP BY</code>. Each column referenced in
  527. <em class="replaceable"><code>condition</code></em> must
  528. unambiguously reference a grouping column, unless the reference
  529. appears within an aggregate function or the ungrouped column is
  530. functionally dependent on the grouping columns.
  531. </p><p>
  532. The presence of <code class="literal">HAVING</code> turns a query into a grouped
  533. query even if there is no <code class="literal">GROUP BY</code> clause. This is the
  534. same as what happens when the query contains aggregate functions but
  535. no <code class="literal">GROUP BY</code> clause. All the selected rows are considered to
  536. form a single group, and the <code class="command">SELECT</code> list and
  537. <code class="literal">HAVING</code> clause can only reference table columns from
  538. within aggregate functions. Such a query will emit a single row if the
  539. <code class="literal">HAVING</code> condition is true, zero rows if it is not true.
  540. </p><p>
  541. Currently, <code class="literal">FOR NO KEY UPDATE</code>, <code class="literal">FOR UPDATE</code>,
  542. <code class="literal">FOR SHARE</code> and <code class="literal">FOR KEY SHARE</code> cannot be
  543. specified with <code class="literal">HAVING</code>.
  544. </p></div><div class="refsect2" id="SQL-WINDOW"><h3><code class="literal">WINDOW</code> Clause</h3><p>
  545. The optional <code class="literal">WINDOW</code> clause has the general form
  546. </p><pre class="synopsis">
  547. WINDOW <em class="replaceable"><code>window_name</code></em> AS ( <em class="replaceable"><code>window_definition</code></em> ) [, ...]
  548. </pre><p>
  549. where <em class="replaceable"><code>window_name</code></em> is
  550. a name that can be referenced from <code class="literal">OVER</code> clauses or
  551. subsequent window definitions, and
  552. <em class="replaceable"><code>window_definition</code></em> is
  553. </p><pre class="synopsis">
  554. [ <em class="replaceable"><code>existing_window_name</code></em> ]
  555. [ PARTITION BY <em class="replaceable"><code>expression</code></em> [, ...] ]
  556. [ ORDER BY <em class="replaceable"><code>expression</code></em> [ ASC | DESC | USING <em class="replaceable"><code>operator</code></em> ] [ NULLS { FIRST | LAST } ] [, ...] ]
  557. [ <em class="replaceable"><code>frame_clause</code></em> ]
  558. </pre><p>
  559. </p><p>
  560. If an <em class="replaceable"><code>existing_window_name</code></em>
  561. is specified it must refer to an earlier entry in the <code class="literal">WINDOW</code>
  562. list; the new window copies its partitioning clause from that entry,
  563. as well as its ordering clause if any. In this case the new window cannot
  564. specify its own <code class="literal">PARTITION BY</code> clause, and it can specify
  565. <code class="literal">ORDER BY</code> only if the copied window does not have one.
  566. The new window always uses its own frame clause; the copied window
  567. must not specify a frame clause.
  568. </p><p>
  569. The elements of the <code class="literal">PARTITION BY</code> list are interpreted in
  570. much the same fashion as elements of a
  571. <a class="xref" href="sql-select.html#SQL-GROUPBY" title="GROUP BY Clause"><code class="literal">GROUP BY</code> Clause</a>, except that
  572. they are always simple expressions and never the name or number of an
  573. output column.
  574. Another difference is that these expressions can contain aggregate
  575. function calls, which are not allowed in a regular <code class="literal">GROUP BY</code>
  576. clause. They are allowed here because windowing occurs after grouping
  577. and aggregation.
  578. </p><p>
  579. Similarly, the elements of the <code class="literal">ORDER BY</code> list are interpreted
  580. in much the same fashion as elements of an
  581. <a class="xref" href="sql-select.html#SQL-ORDERBY" title="ORDER BY Clause"><code class="literal">ORDER BY</code> Clause</a>, except that
  582. the expressions are always taken as simple expressions and never the name
  583. or number of an output column.
  584. </p><p>
  585. The optional <em class="replaceable"><code>frame_clause</code></em> defines
  586. the <em class="firstterm">window frame</em> for window functions that depend on the
  587. frame (not all do). The window frame is a set of related rows for
  588. each row of the query (called the <em class="firstterm">current row</em>).
  589. The <em class="replaceable"><code>frame_clause</code></em> can be one of
  590. </p><pre class="synopsis">
  591. { RANGE | ROWS | GROUPS } <em class="replaceable"><code>frame_start</code></em> [ <em class="replaceable"><code>frame_exclusion</code></em> ]
  592. { RANGE | ROWS | GROUPS } BETWEEN <em class="replaceable"><code>frame_start</code></em> AND <em class="replaceable"><code>frame_end</code></em> [ <em class="replaceable"><code>frame_exclusion</code></em> ]
  593. </pre><p>
  594. where <em class="replaceable"><code>frame_start</code></em>
  595. and <em class="replaceable"><code>frame_end</code></em> can be one of
  596. </p><pre class="synopsis">
  597. UNBOUNDED PRECEDING
  598. <em class="replaceable"><code>offset</code></em> PRECEDING
  599. CURRENT ROW
  600. <em class="replaceable"><code>offset</code></em> FOLLOWING
  601. UNBOUNDED FOLLOWING
  602. </pre><p>
  603. and <em class="replaceable"><code>frame_exclusion</code></em> can be one of
  604. </p><pre class="synopsis">
  605. EXCLUDE CURRENT ROW
  606. EXCLUDE GROUP
  607. EXCLUDE TIES
  608. EXCLUDE NO OTHERS
  609. </pre><p>
  610. If <em class="replaceable"><code>frame_end</code></em> is omitted it defaults to <code class="literal">CURRENT
  611. ROW</code>. Restrictions are that
  612. <em class="replaceable"><code>frame_start</code></em> cannot be <code class="literal">UNBOUNDED FOLLOWING</code>,
  613. <em class="replaceable"><code>frame_end</code></em> cannot be <code class="literal">UNBOUNDED PRECEDING</code>,
  614. and the <em class="replaceable"><code>frame_end</code></em> choice cannot appear earlier in the
  615. above list of <em class="replaceable"><code>frame_start</code></em>
  616. and <em class="replaceable"><code>frame_end</code></em> options than
  617. the <em class="replaceable"><code>frame_start</code></em> choice does — for example
  618. <code class="literal">RANGE BETWEEN CURRENT ROW AND <em class="replaceable"><code>offset</code></em>
  619. PRECEDING</code> is not allowed.
  620. </p><p>
  621. The default framing option is <code class="literal">RANGE UNBOUNDED PRECEDING</code>,
  622. which is the same as <code class="literal">RANGE BETWEEN UNBOUNDED PRECEDING AND
  623. CURRENT ROW</code>; it sets the frame to be all rows from the partition start
  624. up through the current row's last <em class="firstterm">peer</em> (a row
  625. that the window's <code class="literal">ORDER BY</code> clause considers
  626. equivalent to the current row; all rows are peers if there
  627. is no <code class="literal">ORDER BY</code>).
  628. In general, <code class="literal">UNBOUNDED PRECEDING</code> means that the frame
  629. starts with the first row of the partition, and similarly
  630. <code class="literal">UNBOUNDED FOLLOWING</code> means that the frame ends with the last
  631. row of the partition, regardless
  632. of <code class="literal">RANGE</code>, <code class="literal">ROWS</code>
  633. or <code class="literal">GROUPS</code> mode.
  634. In <code class="literal">ROWS</code> mode, <code class="literal">CURRENT ROW</code> means
  635. that the frame starts or ends with the current row; but
  636. in <code class="literal">RANGE</code> or <code class="literal">GROUPS</code> mode it means
  637. that the frame starts or ends with the current row's first or last peer
  638. in the <code class="literal">ORDER BY</code> ordering.
  639. The <em class="replaceable"><code>offset</code></em> <code class="literal">PRECEDING</code> and
  640. <em class="replaceable"><code>offset</code></em> <code class="literal">FOLLOWING</code> options
  641. vary in meaning depending on the frame mode.
  642. In <code class="literal">ROWS</code> mode, the <em class="replaceable"><code>offset</code></em>
  643. is an integer indicating that the frame starts or ends that many rows
  644. before or after the current row.
  645. In <code class="literal">GROUPS</code> mode, the <em class="replaceable"><code>offset</code></em>
  646. is an integer indicating that the frame starts or ends that many peer
  647. groups before or after the current row's peer group, where
  648. a <em class="firstterm">peer group</em> is a group of rows that are
  649. equivalent according to the window's <code class="literal">ORDER BY</code> clause.
  650. In <code class="literal">RANGE</code> mode, use of
  651. an <em class="replaceable"><code>offset</code></em> option requires that there be
  652. exactly one <code class="literal">ORDER BY</code> column in the window definition.
  653. Then the frame contains those rows whose ordering column value is no
  654. more than <em class="replaceable"><code>offset</code></em> less than
  655. (for <code class="literal">PRECEDING</code>) or more than
  656. (for <code class="literal">FOLLOWING</code>) the current row's ordering column
  657. value. In these cases the data type of
  658. the <em class="replaceable"><code>offset</code></em> expression depends on the data
  659. type of the ordering column. For numeric ordering columns it is
  660. typically of the same type as the ordering column, but for datetime
  661. ordering columns it is an <code class="type">interval</code>.
  662. In all these cases, the value of the <em class="replaceable"><code>offset</code></em>
  663. must be non-null and non-negative. Also, while
  664. the <em class="replaceable"><code>offset</code></em> does not have to be a simple
  665. constant, it cannot contain variables, aggregate functions, or window
  666. functions.
  667. </p><p>
  668. The <em class="replaceable"><code>frame_exclusion</code></em> option allows rows around
  669. the current row to be excluded from the frame, even if they would be
  670. included according to the frame start and frame end options.
  671. <code class="literal">EXCLUDE CURRENT ROW</code> excludes the current row from the
  672. frame.
  673. <code class="literal">EXCLUDE GROUP</code> excludes the current row and its
  674. ordering peers from the frame.
  675. <code class="literal">EXCLUDE TIES</code> excludes any peers of the current
  676. row from the frame, but not the current row itself.
  677. <code class="literal">EXCLUDE NO OTHERS</code> simply specifies explicitly the
  678. default behavior of not excluding the current row or its peers.
  679. </p><p>
  680. Beware that the <code class="literal">ROWS</code> mode can produce unpredictable
  681. results if the <code class="literal">ORDER BY</code> ordering does not order the rows
  682. uniquely. The <code class="literal">RANGE</code> and <code class="literal">GROUPS</code>
  683. modes are designed to ensure that rows that are peers in
  684. the <code class="literal">ORDER BY</code> ordering are treated alike: all rows of
  685. a given peer group will be in the frame or excluded from it.
  686. </p><p>
  687. The purpose of a <code class="literal">WINDOW</code> clause is to specify the
  688. behavior of <em class="firstterm">window functions</em> appearing in the query's
  689. <a class="xref" href="sql-select.html#SQL-SELECT-LIST" title="SELECT List"><code class="command">SELECT</code> List</a> or
  690. <a class="xref" href="sql-select.html#SQL-ORDERBY" title="ORDER BY Clause"><code class="literal">ORDER BY</code> Clause</a>. These functions
  691. can reference the <code class="literal">WINDOW</code> clause entries by name
  692. in their <code class="literal">OVER</code> clauses. A <code class="literal">WINDOW</code> clause
  693. entry does not have to be referenced anywhere, however; if it is not
  694. used in the query it is simply ignored. It is possible to use window
  695. functions without any <code class="literal">WINDOW</code> clause at all, since
  696. a window function call can specify its window definition directly in
  697. its <code class="literal">OVER</code> clause. However, the <code class="literal">WINDOW</code>
  698. clause saves typing when the same window definition is needed for more
  699. than one window function.
  700. </p><p>
  701. Currently, <code class="literal">FOR NO KEY UPDATE</code>, <code class="literal">FOR UPDATE</code>,
  702. <code class="literal">FOR SHARE</code> and <code class="literal">FOR KEY SHARE</code> cannot be
  703. specified with <code class="literal">WINDOW</code>.
  704. </p><p>
  705. Window functions are described in detail in
  706. <a class="xref" href="tutorial-window.html" title="3.5. Window Functions">Section 3.5</a>,
  707. <a class="xref" href="sql-expressions.html#SYNTAX-WINDOW-FUNCTIONS" title="4.2.8. Window Function Calls">Section 4.2.8</a>, and
  708. <a class="xref" href="queries-table-expressions.html#QUERIES-WINDOW" title="7.2.5. Window Function Processing">Section 7.2.5</a>.
  709. </p></div><div class="refsect2" id="SQL-SELECT-LIST"><h3><code class="command">SELECT</code> List</h3><p>
  710. The <code class="command">SELECT</code> list (between the key words
  711. <code class="literal">SELECT</code> and <code class="literal">FROM</code>) specifies expressions
  712. that form the output rows of the <code class="command">SELECT</code>
  713. statement. The expressions can (and usually do) refer to columns
  714. computed in the <code class="literal">FROM</code> clause.
  715. </p><p>
  716. Just as in a table, every output column of a <code class="command">SELECT</code>
  717. has a name. In a simple <code class="command">SELECT</code> this name is just
  718. used to label the column for display, but when the <code class="command">SELECT</code>
  719. is a sub-query of a larger query, the name is seen by the larger query
  720. as the column name of the virtual table produced by the sub-query.
  721. To specify the name to use for an output column, write
  722. <code class="literal">AS</code> <em class="replaceable"><code>output_name</code></em>
  723. after the column's expression. (You can omit <code class="literal">AS</code>,
  724. but only if the desired output name does not match any
  725. <span class="productname">PostgreSQL</span> keyword (see <a class="xref" href="sql-keywords-appendix.html" title="Appendix C. SQL Key Words">Appendix C</a>). For protection against possible
  726. future keyword additions, it is recommended that you always either
  727. write <code class="literal">AS</code> or double-quote the output name.)
  728. If you do not specify a column name, a name is chosen automatically
  729. by <span class="productname">PostgreSQL</span>. If the column's expression
  730. is a simple column reference then the chosen name is the same as that
  731. column's name. In more complex cases a function or type name may be
  732. used, or the system may fall back on a generated name such as
  733. <code class="literal">?column?</code>.
  734. </p><p>
  735. An output column's name can be used to refer to the column's value in
  736. <code class="literal">ORDER BY</code> and <code class="literal">GROUP BY</code> clauses, but not in the
  737. <code class="literal">WHERE</code> or <code class="literal">HAVING</code> clauses; there you must write
  738. out the expression instead.
  739. </p><p>
  740. Instead of an expression, <code class="literal">*</code> can be written in
  741. the output list as a shorthand for all the columns of the selected
  742. rows. Also, you can write <code class="literal"><em class="replaceable"><code>table_name</code></em>.*</code> as a
  743. shorthand for the columns coming from just that table. In these
  744. cases it is not possible to specify new names with <code class="literal">AS</code>;
  745. the output column names will be the same as the table columns' names.
  746. </p><p>
  747. According to the SQL standard, the expressions in the output list should
  748. be computed before applying <code class="literal">DISTINCT</code>, <code class="literal">ORDER
  749. BY</code>, or <code class="literal">LIMIT</code>. This is obviously necessary
  750. when using <code class="literal">DISTINCT</code>, since otherwise it's not clear
  751. what values are being made distinct. However, in many cases it is
  752. convenient if output expressions are computed after <code class="literal">ORDER
  753. BY</code> and <code class="literal">LIMIT</code>; particularly if the output list
  754. contains any volatile or expensive functions. With that behavior, the
  755. order of function evaluations is more intuitive and there will not be
  756. evaluations corresponding to rows that never appear in the output.
  757. <span class="productname">PostgreSQL</span> will effectively evaluate output expressions
  758. after sorting and limiting, so long as those expressions are not
  759. referenced in <code class="literal">DISTINCT</code>, <code class="literal">ORDER BY</code>
  760. or <code class="literal">GROUP BY</code>. (As a counterexample, <code class="literal">SELECT
  761. f(x) FROM tab ORDER BY 1</code> clearly must evaluate <code class="function">f(x)</code>
  762. before sorting.) Output expressions that contain set-returning functions
  763. are effectively evaluated after sorting and before limiting, so
  764. that <code class="literal">LIMIT</code> will act to cut off the output from a
  765. set-returning function.
  766. </p><div class="note"><h3 class="title">Note</h3><p>
  767. <span class="productname">PostgreSQL</span> versions before 9.6 did not provide any
  768. guarantees about the timing of evaluation of output expressions versus
  769. sorting and limiting; it depended on the form of the chosen query plan.
  770. </p></div></div><div class="refsect2" id="SQL-DISTINCT"><h3><code class="literal">DISTINCT</code> Clause</h3><p>
  771. If <code class="literal">SELECT DISTINCT</code> is specified, all duplicate rows are
  772. removed from the result set (one row is kept from each group of
  773. duplicates). <code class="literal">SELECT ALL</code> specifies the opposite: all rows are
  774. kept; that is the default.
  775. </p><p>
  776. <code class="literal">SELECT DISTINCT ON ( <em class="replaceable"><code>expression</code></em> [, ...] )</code>
  777. keeps only the first row of each set of rows where the given
  778. expressions evaluate to equal. The <code class="literal">DISTINCT ON</code>
  779. expressions are interpreted using the same rules as for
  780. <code class="literal">ORDER BY</code> (see above). Note that the <span class="quote">“<span class="quote">first
  781. row</span>”</span> of each set is unpredictable unless <code class="literal">ORDER
  782. BY</code> is used to ensure that the desired row appears first. For
  783. example:
  784. </p><pre class="programlisting">
  785. SELECT DISTINCT ON (location) location, time, report
  786. FROM weather_reports
  787. ORDER BY location, time DESC;
  788. </pre><p>
  789. retrieves the most recent weather report for each location. But
  790. if we had not used <code class="literal">ORDER BY</code> to force descending order
  791. of time values for each location, we'd have gotten a report from
  792. an unpredictable time for each location.
  793. </p><p>
  794. The <code class="literal">DISTINCT ON</code> expression(s) must match the leftmost
  795. <code class="literal">ORDER BY</code> expression(s). The <code class="literal">ORDER BY</code> clause
  796. will normally contain additional expression(s) that determine the
  797. desired precedence of rows within each <code class="literal">DISTINCT ON</code> group.
  798. </p><p>
  799. Currently, <code class="literal">FOR NO KEY UPDATE</code>, <code class="literal">FOR UPDATE</code>,
  800. <code class="literal">FOR SHARE</code> and <code class="literal">FOR KEY SHARE</code> cannot be
  801. specified with <code class="literal">DISTINCT</code>.
  802. </p></div><div class="refsect2" id="SQL-UNION"><h3><code class="literal">UNION</code> Clause</h3><p>
  803. The <code class="literal">UNION</code> clause has this general form:
  804. </p><pre class="synopsis">
  805. <em class="replaceable"><code>select_statement</code></em> UNION [ ALL | DISTINCT ] <em class="replaceable"><code>select_statement</code></em>
  806. </pre><p><em class="replaceable"><code>select_statement</code></em> is
  807. any <code class="command">SELECT</code> statement without an <code class="literal">ORDER
  808. BY</code>, <code class="literal">LIMIT</code>, <code class="literal">FOR NO KEY UPDATE</code>, <code class="literal">FOR UPDATE</code>,
  809. <code class="literal">FOR SHARE</code>, or <code class="literal">FOR KEY SHARE</code> clause.
  810. (<code class="literal">ORDER BY</code> and <code class="literal">LIMIT</code> can be attached to a
  811. subexpression if it is enclosed in parentheses. Without
  812. parentheses, these clauses will be taken to apply to the result of
  813. the <code class="literal">UNION</code>, not to its right-hand input
  814. expression.)
  815. </p><p>
  816. The <code class="literal">UNION</code> operator computes the set union of
  817. the rows returned by the involved <code class="command">SELECT</code>
  818. statements. A row is in the set union of two result sets if it
  819. appears in at least one of the result sets. The two
  820. <code class="command">SELECT</code> statements that represent the direct
  821. operands of the <code class="literal">UNION</code> must produce the same
  822. number of columns, and corresponding columns must be of compatible
  823. data types.
  824. </p><p>
  825. The result of <code class="literal">UNION</code> does not contain any duplicate
  826. rows unless the <code class="literal">ALL</code> option is specified.
  827. <code class="literal">ALL</code> prevents elimination of duplicates. (Therefore,
  828. <code class="literal">UNION ALL</code> is usually significantly quicker than
  829. <code class="literal">UNION</code>; use <code class="literal">ALL</code> when you can.)
  830. <code class="literal">DISTINCT</code> can be written to explicitly specify the
  831. default behavior of eliminating duplicate rows.
  832. </p><p>
  833. Multiple <code class="literal">UNION</code> operators in the same
  834. <code class="command">SELECT</code> statement are evaluated left to right,
  835. unless otherwise indicated by parentheses.
  836. </p><p>
  837. Currently, <code class="literal">FOR NO KEY UPDATE</code>, <code class="literal">FOR UPDATE</code>, <code class="literal">FOR SHARE</code> and
  838. <code class="literal">FOR KEY SHARE</code> cannot be
  839. specified either for a <code class="literal">UNION</code> result or for any input of a
  840. <code class="literal">UNION</code>.
  841. </p></div><div class="refsect2" id="SQL-INTERSECT"><h3><code class="literal">INTERSECT</code> Clause</h3><p>
  842. The <code class="literal">INTERSECT</code> clause has this general form:
  843. </p><pre class="synopsis">
  844. <em class="replaceable"><code>select_statement</code></em> INTERSECT [ ALL | DISTINCT ] <em class="replaceable"><code>select_statement</code></em>
  845. </pre><p><em class="replaceable"><code>select_statement</code></em> is
  846. any <code class="command">SELECT</code> statement without an <code class="literal">ORDER
  847. BY</code>, <code class="literal">LIMIT</code>, <code class="literal">FOR NO KEY UPDATE</code>, <code class="literal">FOR UPDATE</code>,
  848. <code class="literal">FOR SHARE</code>, or <code class="literal">FOR KEY SHARE</code> clause.
  849. </p><p>
  850. The <code class="literal">INTERSECT</code> operator computes the set
  851. intersection of the rows returned by the involved
  852. <code class="command">SELECT</code> statements. A row is in the
  853. intersection of two result sets if it appears in both result sets.
  854. </p><p>
  855. The result of <code class="literal">INTERSECT</code> does not contain any
  856. duplicate rows unless the <code class="literal">ALL</code> option is specified.
  857. With <code class="literal">ALL</code>, a row that has <em class="replaceable"><code>m</code></em> duplicates in the
  858. left table and <em class="replaceable"><code>n</code></em> duplicates in the right table will appear
  859. min(<em class="replaceable"><code>m</code></em>,<em class="replaceable"><code>n</code></em>) times in the result set.
  860. <code class="literal">DISTINCT</code> can be written to explicitly specify the
  861. default behavior of eliminating duplicate rows.
  862. </p><p>
  863. Multiple <code class="literal">INTERSECT</code> operators in the same
  864. <code class="command">SELECT</code> statement are evaluated left to right,
  865. unless parentheses dictate otherwise.
  866. <code class="literal">INTERSECT</code> binds more tightly than
  867. <code class="literal">UNION</code>. That is, <code class="literal">A UNION B INTERSECT
  868. C</code> will be read as <code class="literal">A UNION (B INTERSECT
  869. C)</code>.
  870. </p><p>
  871. Currently, <code class="literal">FOR NO KEY UPDATE</code>, <code class="literal">FOR UPDATE</code>, <code class="literal">FOR SHARE</code> and
  872. <code class="literal">FOR KEY SHARE</code> cannot be
  873. specified either for an <code class="literal">INTERSECT</code> result or for any input of
  874. an <code class="literal">INTERSECT</code>.
  875. </p></div><div class="refsect2" id="SQL-EXCEPT"><h3><code class="literal">EXCEPT</code> Clause</h3><p>
  876. The <code class="literal">EXCEPT</code> clause has this general form:
  877. </p><pre class="synopsis">
  878. <em class="replaceable"><code>select_statement</code></em> EXCEPT [ ALL | DISTINCT ] <em class="replaceable"><code>select_statement</code></em>
  879. </pre><p><em class="replaceable"><code>select_statement</code></em> is
  880. any <code class="command">SELECT</code> statement without an <code class="literal">ORDER
  881. BY</code>, <code class="literal">LIMIT</code>, <code class="literal">FOR NO KEY UPDATE</code>, <code class="literal">FOR UPDATE</code>,
  882. <code class="literal">FOR SHARE</code>, or <code class="literal">FOR KEY SHARE</code> clause.
  883. </p><p>
  884. The <code class="literal">EXCEPT</code> operator computes the set of rows
  885. that are in the result of the left <code class="command">SELECT</code>
  886. statement but not in the result of the right one.
  887. </p><p>
  888. The result of <code class="literal">EXCEPT</code> does not contain any
  889. duplicate rows unless the <code class="literal">ALL</code> option is specified.
  890. With <code class="literal">ALL</code>, a row that has <em class="replaceable"><code>m</code></em> duplicates in the
  891. left table and <em class="replaceable"><code>n</code></em> duplicates in the right table will appear
  892. max(<em class="replaceable"><code>m</code></em>-<em class="replaceable"><code>n</code></em>,0) times in the result set.
  893. <code class="literal">DISTINCT</code> can be written to explicitly specify the
  894. default behavior of eliminating duplicate rows.
  895. </p><p>
  896. Multiple <code class="literal">EXCEPT</code> operators in the same
  897. <code class="command">SELECT</code> statement are evaluated left to right,
  898. unless parentheses dictate otherwise. <code class="literal">EXCEPT</code> binds at
  899. the same level as <code class="literal">UNION</code>.
  900. </p><p>
  901. Currently, <code class="literal">FOR NO KEY UPDATE</code>, <code class="literal">FOR UPDATE</code>, <code class="literal">FOR SHARE</code> and
  902. <code class="literal">FOR KEY SHARE</code> cannot be
  903. specified either for an <code class="literal">EXCEPT</code> result or for any input of
  904. an <code class="literal">EXCEPT</code>.
  905. </p></div><div class="refsect2" id="SQL-ORDERBY"><h3><code class="literal">ORDER BY</code> Clause</h3><p>
  906. The optional <code class="literal">ORDER BY</code> clause has this general form:
  907. </p><pre class="synopsis">
  908. ORDER BY <em class="replaceable"><code>expression</code></em> [ ASC | DESC | USING <em class="replaceable"><code>operator</code></em> ] [ NULLS { FIRST | LAST } ] [, ...]
  909. </pre><p>
  910. The <code class="literal">ORDER BY</code> clause causes the result rows to
  911. be sorted according to the specified expression(s). If two rows are
  912. equal according to the leftmost expression, they are compared
  913. according to the next expression and so on. If they are equal
  914. according to all specified expressions, they are returned in
  915. an implementation-dependent order.
  916. </p><p>
  917. Each <em class="replaceable"><code>expression</code></em> can be the
  918. name or ordinal number of an output column
  919. (<code class="command">SELECT</code> list item), or it can be an arbitrary
  920. expression formed from input-column values.
  921. </p><p>
  922. The ordinal number refers to the ordinal (left-to-right) position
  923. of the output column. This feature makes it possible to define an
  924. ordering on the basis of a column that does not have a unique
  925. name. This is never absolutely necessary because it is always
  926. possible to assign a name to an output column using the
  927. <code class="literal">AS</code> clause.
  928. </p><p>
  929. It is also possible to use arbitrary expressions in the
  930. <code class="literal">ORDER BY</code> clause, including columns that do not
  931. appear in the <code class="command">SELECT</code> output list. Thus the
  932. following statement is valid:
  933. </p><pre class="programlisting">
  934. SELECT name FROM distributors ORDER BY code;
  935. </pre><p>
  936. A limitation of this feature is that an <code class="literal">ORDER BY</code>
  937. clause applying to the result of a <code class="literal">UNION</code>,
  938. <code class="literal">INTERSECT</code>, or <code class="literal">EXCEPT</code> clause can only
  939. specify an output column name or number, not an expression.
  940. </p><p>
  941. If an <code class="literal">ORDER BY</code> expression is a simple name that
  942. matches both an output column name and an input column name,
  943. <code class="literal">ORDER BY</code> will interpret it as the output column name.
  944. This is the opposite of the choice that <code class="literal">GROUP BY</code> will
  945. make in the same situation. This inconsistency is made to be
  946. compatible with the SQL standard.
  947. </p><p>
  948. Optionally one can add the key word <code class="literal">ASC</code> (ascending) or
  949. <code class="literal">DESC</code> (descending) after any expression in the
  950. <code class="literal">ORDER BY</code> clause. If not specified, <code class="literal">ASC</code> is
  951. assumed by default. Alternatively, a specific ordering operator
  952. name can be specified in the <code class="literal">USING</code> clause.
  953. An ordering operator must be a less-than or greater-than
  954. member of some B-tree operator family.
  955. <code class="literal">ASC</code> is usually equivalent to <code class="literal">USING &lt;</code> and
  956. <code class="literal">DESC</code> is usually equivalent to <code class="literal">USING &gt;</code>.
  957. (But the creator of a user-defined data type can define exactly what the
  958. default sort ordering is, and it might correspond to operators with other
  959. names.)
  960. </p><p>
  961. If <code class="literal">NULLS LAST</code> is specified, null values sort after all
  962. non-null values; if <code class="literal">NULLS FIRST</code> is specified, null values
  963. sort before all non-null values. If neither is specified, the default
  964. behavior is <code class="literal">NULLS LAST</code> when <code class="literal">ASC</code> is specified
  965. or implied, and <code class="literal">NULLS FIRST</code> when <code class="literal">DESC</code> is specified
  966. (thus, the default is to act as though nulls are larger than non-nulls).
  967. When <code class="literal">USING</code> is specified, the default nulls ordering depends
  968. on whether the operator is a less-than or greater-than operator.
  969. </p><p>
  970. Note that ordering options apply only to the expression they follow;
  971. for example <code class="literal">ORDER BY x, y DESC</code> does not mean
  972. the same thing as <code class="literal">ORDER BY x DESC, y DESC</code>.
  973. </p><p>
  974. Character-string data is sorted according to the collation that applies
  975. to the column being sorted. That can be overridden at need by including
  976. a <code class="literal">COLLATE</code> clause in the
  977. <em class="replaceable"><code>expression</code></em>, for example
  978. <code class="literal">ORDER BY mycolumn COLLATE "en_US"</code>.
  979. For more information see <a class="xref" href="sql-expressions.html#SQL-SYNTAX-COLLATE-EXPRS" title="4.2.10. Collation Expressions">Section 4.2.10</a> and
  980. <a class="xref" href="collation.html" title="23.2. Collation Support">Section 23.2</a>.
  981. </p></div><div class="refsect2" id="SQL-LIMIT"><h3><code class="literal">LIMIT</code> Clause</h3><p>
  982. The <code class="literal">LIMIT</code> clause consists of two independent
  983. sub-clauses:
  984. </p><pre class="synopsis">
  985. LIMIT { <em class="replaceable"><code>count</code></em> | ALL }
  986. OFFSET <em class="replaceable"><code>start</code></em>
  987. </pre><p>
  988. <em class="replaceable"><code>count</code></em> specifies the
  989. maximum number of rows to return, while <em class="replaceable"><code>start</code></em> specifies the number of rows
  990. to skip before starting to return rows. When both are specified,
  991. <em class="replaceable"><code>start</code></em> rows are skipped
  992. before starting to count the <em class="replaceable"><code>count</code></em> rows to be returned.
  993. </p><p>
  994. If the <em class="replaceable"><code>count</code></em> expression
  995. evaluates to NULL, it is treated as <code class="literal">LIMIT ALL</code>, i.e., no
  996. limit. If <em class="replaceable"><code>start</code></em> evaluates
  997. to NULL, it is treated the same as <code class="literal">OFFSET 0</code>.
  998. </p><p>
  999. SQL:2008 introduced a different syntax to achieve the same result,
  1000. which <span class="productname">PostgreSQL</span> also supports. It is:
  1001. </p><pre class="synopsis">
  1002. OFFSET <em class="replaceable"><code>start</code></em> { ROW | ROWS }
  1003. FETCH { FIRST | NEXT } [ <em class="replaceable"><code>count</code></em> ] { ROW | ROWS } ONLY
  1004. </pre><p>
  1005. In this syntax, the <em class="replaceable"><code>start</code></em>
  1006. or <em class="replaceable"><code>count</code></em> value is required by
  1007. the standard to be a literal constant, a parameter, or a variable name;
  1008. as a <span class="productname">PostgreSQL</span> extension, other expressions
  1009. are allowed, but will generally need to be enclosed in parentheses to avoid
  1010. ambiguity.
  1011. If <em class="replaceable"><code>count</code></em> is
  1012. omitted in a <code class="literal">FETCH</code> clause, it defaults to 1.
  1013. <code class="literal">ROW</code>
  1014. and <code class="literal">ROWS</code> as well as <code class="literal">FIRST</code>
  1015. and <code class="literal">NEXT</code> are noise words that don't influence
  1016. the effects of these clauses.
  1017. According to the standard, the <code class="literal">OFFSET</code> clause must come
  1018. before the <code class="literal">FETCH</code> clause if both are present; but
  1019. <span class="productname">PostgreSQL</span> is laxer and allows either order.
  1020. </p><p>
  1021. When using <code class="literal">LIMIT</code>, it is a good idea to use an
  1022. <code class="literal">ORDER BY</code> clause that constrains the result rows into a
  1023. unique order. Otherwise you will get an unpredictable subset of
  1024. the query's rows — you might be asking for the tenth through
  1025. twentieth rows, but tenth through twentieth in what ordering? You
  1026. don't know what ordering unless you specify <code class="literal">ORDER BY</code>.
  1027. </p><p>
  1028. The query planner takes <code class="literal">LIMIT</code> into account when
  1029. generating a query plan, so you are very likely to get different
  1030. plans (yielding different row orders) depending on what you use
  1031. for <code class="literal">LIMIT</code> and <code class="literal">OFFSET</code>. Thus, using
  1032. different <code class="literal">LIMIT</code>/<code class="literal">OFFSET</code> values to select
  1033. different subsets of a query result <span class="emphasis"><em>will give
  1034. inconsistent results</em></span> unless you enforce a predictable
  1035. result ordering with <code class="literal">ORDER BY</code>. This is not a bug; it
  1036. is an inherent consequence of the fact that SQL does not promise
  1037. to deliver the results of a query in any particular order unless
  1038. <code class="literal">ORDER BY</code> is used to constrain the order.
  1039. </p><p>
  1040. It is even possible for repeated executions of the same <code class="literal">LIMIT</code>
  1041. query to return different subsets of the rows of a table, if there
  1042. is not an <code class="literal">ORDER BY</code> to enforce selection of a deterministic
  1043. subset. Again, this is not a bug; determinism of the results is
  1044. simply not guaranteed in such a case.
  1045. </p></div><div class="refsect2" id="SQL-FOR-UPDATE-SHARE"><h3>The Locking Clause</h3><p>
  1046. <code class="literal">FOR UPDATE</code>, <code class="literal">FOR NO KEY UPDATE</code>, <code class="literal">FOR SHARE</code>
  1047. and <code class="literal">FOR KEY SHARE</code>
  1048. are <em class="firstterm">locking clauses</em>; they affect how <code class="literal">SELECT</code>
  1049. locks rows as they are obtained from the table.
  1050. </p><p>
  1051. The locking clause has the general form
  1052. </p><pre class="synopsis">
  1053. FOR <em class="replaceable"><code>lock_strength</code></em> [ OF <em class="replaceable"><code>table_name</code></em> [, ...] ] [ NOWAIT | SKIP LOCKED ]
  1054. </pre><p>
  1055. where <em class="replaceable"><code>lock_strength</code></em> can be one of
  1056. </p><pre class="synopsis">
  1057. UPDATE
  1058. NO KEY UPDATE
  1059. SHARE
  1060. KEY SHARE
  1061. </pre><p>
  1062. </p><p>
  1063. For more information on each row-level lock mode, refer to
  1064. <a class="xref" href="explicit-locking.html#LOCKING-ROWS" title="13.3.2. Row-Level Locks">Section 13.3.2</a>.
  1065. </p><p>
  1066. To prevent the operation from waiting for other transactions to commit,
  1067. use either the <code class="literal">NOWAIT</code> or <code class="literal">SKIP LOCKED</code>
  1068. option. With <code class="literal">NOWAIT</code>, the statement reports an error, rather
  1069. than waiting, if a selected row cannot be locked immediately.
  1070. With <code class="literal">SKIP LOCKED</code>, any selected rows that cannot be
  1071. immediately locked are skipped. Skipping locked rows provides an
  1072. inconsistent view of the data, so this is not suitable for general purpose
  1073. work, but can be used to avoid lock contention with multiple consumers
  1074. accessing a queue-like table.
  1075. Note that <code class="literal">NOWAIT</code> and <code class="literal">SKIP LOCKED</code> apply only
  1076. to the row-level lock(s) — the required <code class="literal">ROW SHARE</code>
  1077. table-level lock is still taken in the ordinary way (see
  1078. <a class="xref" href="mvcc.html" title="Chapter 13. Concurrency Control">Chapter 13</a>). You can use
  1079. <a class="xref" href="sql-lock.html" title="LOCK"><span class="refentrytitle">LOCK</span></a>
  1080. with the <code class="literal">NOWAIT</code> option first,
  1081. if you need to acquire the table-level lock without waiting.
  1082. </p><p>
  1083. If specific tables are named in a locking clause,
  1084. then only rows coming from those tables are locked; any other
  1085. tables used in the <code class="command">SELECT</code> are simply read as
  1086. usual. A locking
  1087. clause without a table list affects all tables used in the statement.
  1088. If a locking clause is
  1089. applied to a view or sub-query, it affects all tables used in
  1090. the view or sub-query.
  1091. However, these clauses
  1092. do not apply to <code class="literal">WITH</code> queries referenced by the primary query.
  1093. If you want row locking to occur within a <code class="literal">WITH</code> query, specify
  1094. a locking clause within the <code class="literal">WITH</code> query.
  1095. </p><p>
  1096. Multiple locking
  1097. clauses can be written if it is necessary to specify different locking
  1098. behavior for different tables. If the same table is mentioned (or
  1099. implicitly affected) by more than one locking clause,
  1100. then it is processed as if it was only specified by the strongest one.
  1101. Similarly, a table is processed
  1102. as <code class="literal">NOWAIT</code> if that is specified in any of the clauses
  1103. affecting it. Otherwise, it is processed
  1104. as <code class="literal">SKIP LOCKED</code> if that is specified in any of the
  1105. clauses affecting it.
  1106. </p><p>
  1107. The locking clauses cannot be
  1108. used in contexts where returned rows cannot be clearly identified with
  1109. individual table rows; for example they cannot be used with aggregation.
  1110. </p><p>
  1111. When a locking clause
  1112. appears at the top level of a <code class="command">SELECT</code> query, the rows that
  1113. are locked are exactly those that are returned by the query; in the
  1114. case of a join query, the rows locked are those that contribute to
  1115. returned join rows. In addition, rows that satisfied the query
  1116. conditions as of the query snapshot will be locked, although they
  1117. will not be returned if they were updated after the snapshot
  1118. and no longer satisfy the query conditions. If a
  1119. <code class="literal">LIMIT</code> is used, locking stops
  1120. once enough rows have been returned to satisfy the limit (but note that
  1121. rows skipped over by <code class="literal">OFFSET</code> will get locked). Similarly,
  1122. if a locking clause
  1123. is used in a cursor's query, only rows actually fetched or stepped past
  1124. by the cursor will be locked.
  1125. </p><p>
  1126. When a locking clause
  1127. appears in a sub-<code class="command">SELECT</code>, the rows locked are those
  1128. returned to the outer query by the sub-query. This might involve
  1129. fewer rows than inspection of the sub-query alone would suggest,
  1130. since conditions from the outer query might be used to optimize
  1131. execution of the sub-query. For example,
  1132. </p><pre class="programlisting">
  1133. SELECT * FROM (SELECT * FROM mytable FOR UPDATE) ss WHERE col1 = 5;
  1134. </pre><p>
  1135. will lock only rows having <code class="literal">col1 = 5</code>, even though that
  1136. condition is not textually within the sub-query.
  1137. </p><p>
  1138. Previous releases failed to preserve a lock which is upgraded by a later
  1139. savepoint. For example, this code:
  1140. </p><pre class="programlisting">
  1141. BEGIN;
  1142. SELECT * FROM mytable WHERE key = 1 FOR UPDATE;
  1143. SAVEPOINT s;
  1144. UPDATE mytable SET ... WHERE key = 1;
  1145. ROLLBACK TO s;
  1146. </pre><p>
  1147. would fail to preserve the <code class="literal">FOR UPDATE</code> lock after the
  1148. <code class="command">ROLLBACK TO</code>. This has been fixed in release 9.3.
  1149. </p><div class="caution"><h3 class="title">Caution</h3><p>
  1150. It is possible for a <code class="command">SELECT</code> command running at the <code class="literal">READ
  1151. COMMITTED</code> transaction isolation level and using <code class="literal">ORDER
  1152. BY</code> and a locking clause to return rows out of
  1153. order. This is because <code class="literal">ORDER BY</code> is applied first.
  1154. The command sorts the result, but might then block trying to obtain a lock
  1155. on one or more of the rows. Once the <code class="literal">SELECT</code> unblocks, some
  1156. of the ordering column values might have been modified, leading to those
  1157. rows appearing to be out of order (though they are in order in terms
  1158. of the original column values). This can be worked around at need by
  1159. placing the <code class="literal">FOR UPDATE/SHARE</code> clause in a sub-query,
  1160. for example
  1161. </p><pre class="programlisting">
  1162. SELECT * FROM (SELECT * FROM mytable FOR UPDATE) ss ORDER BY column1;
  1163. </pre><p>
  1164. Note that this will result in locking all rows of <code class="structname">mytable</code>,
  1165. whereas <code class="literal">FOR UPDATE</code> at the top level would lock only the
  1166. actually returned rows. This can make for a significant performance
  1167. difference, particularly if the <code class="literal">ORDER BY</code> is combined with
  1168. <code class="literal">LIMIT</code> or other restrictions. So this technique is recommended
  1169. only if concurrent updates of the ordering columns are expected and a
  1170. strictly sorted result is required.
  1171. </p><p>
  1172. At the <code class="literal">REPEATABLE READ</code> or <code class="literal">SERIALIZABLE</code>
  1173. transaction isolation level this would cause a serialization failure (with
  1174. a <code class="literal">SQLSTATE</code> of <code class="literal">'40001'</code>), so there is
  1175. no possibility of receiving rows out of order under these isolation levels.
  1176. </p></div></div><div class="refsect2" id="SQL-TABLE"><h3><code class="literal">TABLE</code> Command</h3><p>
  1177. The command
  1178. </p><pre class="programlisting">
  1179. TABLE <em class="replaceable"><code>name</code></em>
  1180. </pre><p>
  1181. is equivalent to
  1182. </p><pre class="programlisting">
  1183. SELECT * FROM <em class="replaceable"><code>name</code></em>
  1184. </pre><p>
  1185. It can be used as a top-level command or as a space-saving syntax
  1186. variant in parts of complex queries. Only the <code class="literal">WITH</code>,
  1187. <code class="literal">UNION</code>, <code class="literal">INTERSECT</code>, <code class="literal">EXCEPT</code>,
  1188. <code class="literal">ORDER BY</code>, <code class="literal">LIMIT</code>, <code class="literal">OFFSET</code>,
  1189. <code class="literal">FETCH</code> and <code class="literal">FOR</code> locking clauses can be used
  1190. with <code class="command">TABLE</code>; the <code class="literal">WHERE</code> clause and any form of
  1191. aggregation cannot
  1192. be used.
  1193. </p></div></div><div class="refsect1" id="id-1.9.3.171.9"><h2>Examples</h2><p>
  1194. To join the table <code class="literal">films</code> with the table
  1195. <code class="literal">distributors</code>:
  1196. </p><pre class="programlisting">
  1197. SELECT f.title, f.did, d.name, f.date_prod, f.kind
  1198. FROM distributors d, films f
  1199. WHERE f.did = d.did
  1200. title | did | name | date_prod | kind
  1201. -------------------+-----+--------------+------------+----------
  1202. The Third Man | 101 | British Lion | 1949-12-23 | Drama
  1203. The African Queen | 101 | British Lion | 1951-08-11 | Romantic
  1204. ...
  1205. </pre><p>
  1206. </p><p>
  1207. To sum the column <code class="literal">len</code> of all films and group
  1208. the results by <code class="literal">kind</code>:
  1209. </p><pre class="programlisting">
  1210. SELECT kind, sum(len) AS total FROM films GROUP BY kind;
  1211. kind | total
  1212. ----------+-------
  1213. Action | 07:34
  1214. Comedy | 02:58
  1215. Drama | 14:28
  1216. Musical | 06:42
  1217. Romantic | 04:38
  1218. </pre><p>
  1219. </p><p>
  1220. To sum the column <code class="literal">len</code> of all films, group
  1221. the results by <code class="literal">kind</code> and show those group totals
  1222. that are less than 5 hours:
  1223. </p><pre class="programlisting">
  1224. SELECT kind, sum(len) AS total
  1225. FROM films
  1226. GROUP BY kind
  1227. HAVING sum(len) &lt; interval '5 hours';
  1228. kind | total
  1229. ----------+-------
  1230. Comedy | 02:58
  1231. Romantic | 04:38
  1232. </pre><p>
  1233. </p><p>
  1234. The following two examples are identical ways of sorting the individual
  1235. results according to the contents of the second column
  1236. (<code class="literal">name</code>):
  1237. </p><pre class="programlisting">
  1238. SELECT * FROM distributors ORDER BY name;
  1239. SELECT * FROM distributors ORDER BY 2;
  1240. did | name
  1241. -----+------------------
  1242. 109 | 20th Century Fox
  1243. 110 | Bavaria Atelier
  1244. 101 | British Lion
  1245. 107 | Columbia
  1246. 102 | Jean Luc Godard
  1247. 113 | Luso films
  1248. 104 | Mosfilm
  1249. 103 | Paramount
  1250. 106 | Toho
  1251. 105 | United Artists
  1252. 111 | Walt Disney
  1253. 112 | Warner Bros.
  1254. 108 | Westward
  1255. </pre><p>
  1256. </p><p>
  1257. The next example shows how to obtain the union of the tables
  1258. <code class="literal">distributors</code> and
  1259. <code class="literal">actors</code>, restricting the results to those that begin
  1260. with the letter W in each table. Only distinct rows are wanted, so the
  1261. key word <code class="literal">ALL</code> is omitted.
  1262. </p><pre class="programlisting">
  1263. distributors: actors:
  1264. did | name id | name
  1265. -----+-------------- ----+----------------
  1266. 108 | Westward 1 | Woody Allen
  1267. 111 | Walt Disney 2 | Warren Beatty
  1268. 112 | Warner Bros. 3 | Walter Matthau
  1269. ... ...
  1270. SELECT distributors.name
  1271. FROM distributors
  1272. WHERE distributors.name LIKE 'W%'
  1273. UNION
  1274. SELECT actors.name
  1275. FROM actors
  1276. WHERE actors.name LIKE 'W%';
  1277. name
  1278. ----------------
  1279. Walt Disney
  1280. Walter Matthau
  1281. Warner Bros.
  1282. Warren Beatty
  1283. Westward
  1284. Woody Allen
  1285. </pre><p>
  1286. </p><p>
  1287. This example shows how to use a function in the <code class="literal">FROM</code>
  1288. clause, both with and without a column definition list:
  1289. </p><pre class="programlisting">
  1290. CREATE FUNCTION distributors(int) RETURNS SETOF distributors AS $$
  1291. SELECT * FROM distributors WHERE did = $1;
  1292. $$ LANGUAGE SQL;
  1293. SELECT * FROM distributors(111);
  1294. did | name
  1295. -----+-------------
  1296. 111 | Walt Disney
  1297. CREATE FUNCTION distributors_2(int) RETURNS SETOF record AS $$
  1298. SELECT * FROM distributors WHERE did = $1;
  1299. $$ LANGUAGE SQL;
  1300. SELECT * FROM distributors_2(111) AS (f1 int, f2 text);
  1301. f1 | f2
  1302. -----+-------------
  1303. 111 | Walt Disney
  1304. </pre><p>
  1305. </p><p>
  1306. Here is an example of a function with an ordinality column added:
  1307. </p><pre class="programlisting">
  1308. SELECT * FROM unnest(ARRAY['a','b','c','d','e','f']) WITH ORDINALITY;
  1309. unnest | ordinality
  1310. --------+----------
  1311. a | 1
  1312. b | 2
  1313. c | 3
  1314. d | 4
  1315. e | 5
  1316. f | 6
  1317. (6 rows)
  1318. </pre><p>
  1319. </p><p>
  1320. This example shows how to use a simple <code class="literal">WITH</code> clause:
  1321. </p><pre class="programlisting">
  1322. WITH t AS (
  1323. SELECT random() as x FROM generate_series(1, 3)
  1324. )
  1325. SELECT * FROM t
  1326. UNION ALL
  1327. SELECT * FROM t
  1328. x
  1329. --------------------
  1330. 0.534150459803641
  1331. 0.520092216785997
  1332. 0.0735620250925422
  1333. 0.534150459803641
  1334. 0.520092216785997
  1335. 0.0735620250925422
  1336. </pre><p>
  1337. Notice that the <code class="literal">WITH</code> query was evaluated only once,
  1338. so that we got two sets of the same three random values.
  1339. </p><p>
  1340. This example uses <code class="literal">WITH RECURSIVE</code> to find all
  1341. subordinates (direct or indirect) of the employee Mary, and their
  1342. level of indirectness, from a table that shows only direct
  1343. subordinates:
  1344. </p><pre class="programlisting">
  1345. WITH RECURSIVE employee_recursive(distance, employee_name, manager_name) AS (
  1346. SELECT 1, employee_name, manager_name
  1347. FROM employee
  1348. WHERE manager_name = 'Mary'
  1349. UNION ALL
  1350. SELECT er.distance + 1, e.employee_name, e.manager_name
  1351. FROM employee_recursive er, employee e
  1352. WHERE er.employee_name = e.manager_name
  1353. )
  1354. SELECT distance, employee_name FROM employee_recursive;
  1355. </pre><p>
  1356. Notice the typical form of recursive queries:
  1357. an initial condition, followed by <code class="literal">UNION</code>,
  1358. followed by the recursive part of the query. Be sure that the
  1359. recursive part of the query will eventually return no tuples, or
  1360. else the query will loop indefinitely. (See <a class="xref" href="queries-with.html" title="7.8. WITH Queries (Common Table Expressions)">Section 7.8</a>
  1361. for more examples.)
  1362. </p><p>
  1363. This example uses <code class="literal">LATERAL</code> to apply a set-returning function
  1364. <code class="function">get_product_names()</code> for each row of the
  1365. <code class="structname">manufacturers</code> table:
  1366. </p><pre class="programlisting">
  1367. SELECT m.name AS mname, pname
  1368. FROM manufacturers m, LATERAL get_product_names(m.id) pname;
  1369. </pre><p>
  1370. Manufacturers not currently having any products would not appear in the
  1371. result, since it is an inner join. If we wished to include the names of
  1372. such manufacturers in the result, we could do:
  1373. </p><pre class="programlisting">
  1374. SELECT m.name AS mname, pname
  1375. FROM manufacturers m LEFT JOIN LATERAL get_product_names(m.id) pname ON true;
  1376. </pre></div><div class="refsect1" id="id-1.9.3.171.10"><h2>Compatibility</h2><p>
  1377. Of course, the <code class="command">SELECT</code> statement is compatible
  1378. with the SQL standard. But there are some extensions and some
  1379. missing features.
  1380. </p><div class="refsect2" id="id-1.9.3.171.10.3"><h3>Omitted <code class="literal">FROM</code> Clauses</h3><p>
  1381. <span class="productname">PostgreSQL</span> allows one to omit the
  1382. <code class="literal">FROM</code> clause. It has a straightforward use to
  1383. compute the results of simple expressions:
  1384. </p><pre class="programlisting">
  1385. SELECT 2+2;
  1386. ?column?
  1387. ----------
  1388. 4
  1389. </pre><p>
  1390. Some other <acronym class="acronym">SQL</acronym> databases cannot do this except
  1391. by introducing a dummy one-row table from which to do the
  1392. <code class="command">SELECT</code>.
  1393. </p><p>
  1394. Note that if a <code class="literal">FROM</code> clause is not specified,
  1395. the query cannot reference any database tables. For example, the
  1396. following query is invalid:
  1397. </p><pre class="programlisting">
  1398. SELECT distributors.* WHERE distributors.name = 'Westward';
  1399. </pre><p>
  1400. <span class="productname">PostgreSQL</span> releases prior to
  1401. 8.1 would accept queries of this form, and add an implicit entry
  1402. to the query's <code class="literal">FROM</code> clause for each table
  1403. referenced by the query. This is no longer allowed.
  1404. </p></div><div class="refsect2" id="id-1.9.3.171.10.4"><h3>Empty <code class="literal">SELECT</code> Lists</h3><p>
  1405. The list of output expressions after <code class="literal">SELECT</code> can be
  1406. empty, producing a zero-column result table.
  1407. This is not valid syntax according to the SQL standard.
  1408. <span class="productname">PostgreSQL</span> allows it to be consistent with
  1409. allowing zero-column tables.
  1410. However, an empty list is not allowed when <code class="literal">DISTINCT</code> is used.
  1411. </p></div><div class="refsect2" id="id-1.9.3.171.10.5"><h3>Omitting the <code class="literal">AS</code> Key Word</h3><p>
  1412. In the SQL standard, the optional key word <code class="literal">AS</code> can be
  1413. omitted before an output column name whenever the new column name
  1414. is a valid column name (that is, not the same as any reserved
  1415. keyword). <span class="productname">PostgreSQL</span> is slightly more
  1416. restrictive: <code class="literal">AS</code> is required if the new column name
  1417. matches any keyword at all, reserved or not. Recommended practice is
  1418. to use <code class="literal">AS</code> or double-quote output column names, to prevent
  1419. any possible conflict against future keyword additions.
  1420. </p><p>
  1421. In <code class="literal">FROM</code> items, both the standard and
  1422. <span class="productname">PostgreSQL</span> allow <code class="literal">AS</code> to
  1423. be omitted before an alias that is an unreserved keyword. But
  1424. this is impractical for output column names, because of syntactic
  1425. ambiguities.
  1426. </p></div><div class="refsect2" id="id-1.9.3.171.10.6"><h3><code class="literal">ONLY</code> and Inheritance</h3><p>
  1427. The SQL standard requires parentheses around the table name when
  1428. writing <code class="literal">ONLY</code>, for example <code class="literal">SELECT * FROM ONLY
  1429. (tab1), ONLY (tab2) WHERE ...</code>. <span class="productname">PostgreSQL</span>
  1430. considers these parentheses to be optional.
  1431. </p><p>
  1432. <span class="productname">PostgreSQL</span> allows a trailing <code class="literal">*</code> to be written to
  1433. explicitly specify the non-<code class="literal">ONLY</code> behavior of including
  1434. child tables. The standard does not allow this.
  1435. </p><p>
  1436. (These points apply equally to all SQL commands supporting the
  1437. <code class="literal">ONLY</code> option.)
  1438. </p></div><div class="refsect2" id="id-1.9.3.171.10.7"><h3><code class="literal">TABLESAMPLE</code> Clause Restrictions</h3><p>
  1439. The <code class="literal">TABLESAMPLE</code> clause is currently accepted only on
  1440. regular tables and materialized views. According to the SQL standard
  1441. it should be possible to apply it to any <code class="literal">FROM</code> item.
  1442. </p></div><div class="refsect2" id="id-1.9.3.171.10.8"><h3>Function Calls in <code class="literal">FROM</code></h3><p>
  1443. <span class="productname">PostgreSQL</span> allows a function call to be
  1444. written directly as a member of the <code class="literal">FROM</code> list. In the SQL
  1445. standard it would be necessary to wrap such a function call in a
  1446. sub-<code class="command">SELECT</code>; that is, the syntax
  1447. <code class="literal">FROM <em class="replaceable"><code>func</code></em>(...) <em class="replaceable"><code>alias</code></em></code>
  1448. is approximately equivalent to
  1449. <code class="literal">FROM LATERAL (SELECT <em class="replaceable"><code>func</code></em>(...)) <em class="replaceable"><code>alias</code></em></code>.
  1450. Note that <code class="literal">LATERAL</code> is considered to be implicit; this is
  1451. because the standard requires <code class="literal">LATERAL</code> semantics for an
  1452. <code class="literal">UNNEST()</code> item in <code class="literal">FROM</code>.
  1453. <span class="productname">PostgreSQL</span> treats <code class="literal">UNNEST()</code> the
  1454. same as other set-returning functions.
  1455. </p></div><div class="refsect2" id="id-1.9.3.171.10.9"><h3>Namespace Available to <code class="literal">GROUP BY</code> and <code class="literal">ORDER BY</code></h3><p>
  1456. In the SQL-92 standard, an <code class="literal">ORDER BY</code> clause can
  1457. only use output column names or numbers, while a <code class="literal">GROUP
  1458. BY</code> clause can only use expressions based on input column
  1459. names. <span class="productname">PostgreSQL</span> extends each of
  1460. these clauses to allow the other choice as well (but it uses the
  1461. standard's interpretation if there is ambiguity).
  1462. <span class="productname">PostgreSQL</span> also allows both clauses to
  1463. specify arbitrary expressions. Note that names appearing in an
  1464. expression will always be taken as input-column names, not as
  1465. output-column names.
  1466. </p><p>
  1467. SQL:1999 and later use a slightly different definition which is not
  1468. entirely upward compatible with SQL-92.
  1469. In most cases, however, <span class="productname">PostgreSQL</span>
  1470. will interpret an <code class="literal">ORDER BY</code> or <code class="literal">GROUP
  1471. BY</code> expression the same way SQL:1999 does.
  1472. </p></div><div class="refsect2" id="id-1.9.3.171.10.10"><h3>Functional Dependencies</h3><p>
  1473. <span class="productname">PostgreSQL</span> recognizes functional dependency
  1474. (allowing columns to be omitted from <code class="literal">GROUP BY</code>) only when
  1475. a table's primary key is included in the <code class="literal">GROUP BY</code> list.
  1476. The SQL standard specifies additional conditions that should be
  1477. recognized.
  1478. </p></div><div class="refsect2" id="id-1.9.3.171.10.11"><h3><code class="literal">LIMIT</code> and <code class="literal">OFFSET</code></h3><p>
  1479. The clauses <code class="literal">LIMIT</code> and <code class="literal">OFFSET</code>
  1480. are <span class="productname">PostgreSQL</span>-specific syntax, also
  1481. used by <span class="productname">MySQL</span>. The SQL:2008 standard
  1482. has introduced the clauses <code class="literal">OFFSET ... FETCH {FIRST|NEXT}
  1483. ...</code> for the same functionality, as shown above
  1484. in <a class="xref" href="sql-select.html#SQL-LIMIT" title="LIMIT Clause"><code class="literal">LIMIT</code> Clause</a>. This
  1485. syntax is also used by <span class="productname">IBM DB2</span>.
  1486. (Applications written for <span class="productname">Oracle</span>
  1487. frequently use a workaround involving the automatically
  1488. generated <code class="literal">rownum</code> column, which is not available in
  1489. PostgreSQL, to implement the effects of these clauses.)
  1490. </p></div><div class="refsect2" id="id-1.9.3.171.10.12"><h3><code class="literal">FOR NO KEY UPDATE</code>, <code class="literal">FOR UPDATE</code>, <code class="literal">FOR SHARE</code>, <code class="literal">FOR KEY SHARE</code></h3><p>
  1491. Although <code class="literal">FOR UPDATE</code> appears in the SQL standard, the
  1492. standard allows it only as an option of <code class="command">DECLARE CURSOR</code>.
  1493. <span class="productname">PostgreSQL</span> allows it in any <code class="command">SELECT</code>
  1494. query as well as in sub-<code class="command">SELECT</code>s, but this is an extension.
  1495. The <code class="literal">FOR NO KEY UPDATE</code>, <code class="literal">FOR SHARE</code> and
  1496. <code class="literal">FOR KEY SHARE</code> variants, as well as the <code class="literal">NOWAIT</code>
  1497. and <code class="literal">SKIP LOCKED</code> options, do not appear in the
  1498. standard.
  1499. </p></div><div class="refsect2" id="id-1.9.3.171.10.13"><h3>Data-Modifying Statements in <code class="literal">WITH</code></h3><p>
  1500. <span class="productname">PostgreSQL</span> allows <code class="command">INSERT</code>,
  1501. <code class="command">UPDATE</code>, and <code class="command">DELETE</code> to be used as <code class="literal">WITH</code>
  1502. queries. This is not found in the SQL standard.
  1503. </p></div><div class="refsect2" id="id-1.9.3.171.10.14"><h3>Nonstandard Clauses</h3><p>
  1504. <code class="literal">DISTINCT ON ( ... )</code> is an extension of the
  1505. SQL standard.
  1506. </p><p>
  1507. <code class="literal">ROWS FROM( ... )</code> is an extension of the SQL standard.
  1508. </p><p>
  1509. The <code class="literal">MATERIALIZED</code> and <code class="literal">NOT
  1510. MATERIALIZED</code> options of <code class="literal">WITH</code> are extensions
  1511. of the SQL standard.
  1512. </p></div></div></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="sql-security-label.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="sql-commands.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="sql-selectinto.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">SECURITY LABEL </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> SELECT INTO</td></tr></table></div></body></html>
上海开阖软件有限公司 沪ICP备12045867号-1