]> begriffs open source - ai-pg/blob - full-docs/html/sql-values.html
Include links to all subsection html pages, with shorter paths too
[ai-pg] / full-docs / html / sql-values.html
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>VALUES</title><link rel="stylesheet" type="text/css" href="stylesheet.css" /><link rev="made" href="pgsql-docs@lists.postgresql.org" /><meta name="generator" content="DocBook XSL Stylesheets Vsnapshot" /><link rel="prev" href="sql-vacuum.html" title="VACUUM" /><link rel="next" href="reference-client.html" title="PostgreSQL Client Applications" /></head><body id="docContent" class="container-fluid col-10"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="5" align="center">VALUES</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="sql-vacuum.html" title="VACUUM">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 18.0 Documentation">Home</a></td><td width="10%" align="right"> <a accesskey="n" href="reference-client.html" title="PostgreSQL Client Applications">Next</a></td></tr></table><hr /></div><div class="refentry" id="SQL-VALUES"><div class="titlepage"></div><a id="id-1.9.3.185.1" class="indexterm"></a><div class="refnamediv"><h2><span class="refentrytitle">VALUES</span></h2><p>VALUES — compute a set of rows</p></div><div class="refsynopsisdiv"><h2>Synopsis</h2><pre class="synopsis">
3 VALUES ( <em class="replaceable"><code>expression</code></em> [, ...] ) [, ...]
4     [ ORDER BY <em class="replaceable"><code>sort_expression</code></em> [ ASC | DESC | USING <em class="replaceable"><code>operator</code></em> ] [, ...] ]
5     [ LIMIT { <em class="replaceable"><code>count</code></em> | ALL } ]
6     [ OFFSET <em class="replaceable"><code>start</code></em> [ ROW | ROWS ] ]
7     [ FETCH { FIRST | NEXT } [ <em class="replaceable"><code>count</code></em> ] { ROW | ROWS } ONLY ]
8 </pre></div><div class="refsect1" id="id-1.9.3.185.5"><h2>Description</h2><p>
9    <code class="command">VALUES</code> computes a row value or set of row values
10    specified by value expressions.  It is most commonly used to generate
11    a <span class="quote">“<span class="quote">constant table</span>”</span> within a larger command, but it can be
12    used on its own.
13   </p><p>
14    When more than one row is specified, all the rows must have the same
15    number of elements.  The data types of the resulting table's columns are
16    determined by combining the explicit or inferred types of the expressions
17    appearing in that column, using the same rules as for <code class="literal">UNION</code>
18    (see <a class="xref" href="typeconv-union-case.html" title="10.5. UNION, CASE, and Related Constructs">Section 10.5</a>).
19   </p><p>
20    Within larger commands, <code class="command">VALUES</code> is syntactically allowed
21    anywhere that <code class="command">SELECT</code> is.  Because it is treated like a
22    <code class="command">SELECT</code> by the grammar, it is possible to use
23    the <code class="literal">ORDER BY</code>, <code class="literal">LIMIT</code> (or
24    equivalently <code class="literal">FETCH FIRST</code>),
25    and <code class="literal">OFFSET</code> clauses with a
26    <code class="command">VALUES</code> command.
27   </p></div><div class="refsect1" id="id-1.9.3.185.6"><h2>Parameters</h2><div class="variablelist"><dl class="variablelist"><dt><span class="term"><em class="replaceable"><code>expression</code></em></span></dt><dd><p>
28       A constant or expression to compute and insert at the indicated place
29       in the resulting table (set of rows).  In a <code class="command">VALUES</code> list
30       appearing at the top level of an <code class="command">INSERT</code>, an
31       <em class="replaceable"><code>expression</code></em> can be replaced
32       by <code class="literal">DEFAULT</code> to indicate that the destination column's
33       default value should be inserted.  <code class="literal">DEFAULT</code> cannot
34       be used when <code class="command">VALUES</code> appears in other contexts.
35      </p></dd><dt><span class="term"><em class="replaceable"><code>sort_expression</code></em></span></dt><dd><p>
36       An expression or integer constant indicating how to sort the result
37       rows.  This expression can refer to the columns of the
38       <code class="command">VALUES</code> result as <code class="literal">column1</code>, <code class="literal">column2</code>,
39       etc.  For more details see
40       <a class="xref" href="sql-select.html#SQL-ORDERBY" title="ORDER BY Clause">ORDER BY Clause</a>
41       in the <a class="xref" href="sql-select.html" title="SELECT"><span class="refentrytitle">SELECT</span></a> documentation.
42      </p></dd><dt><span class="term"><em class="replaceable"><code>operator</code></em></span></dt><dd><p>
43       A sorting operator.  For details see
44       <a class="xref" href="sql-select.html#SQL-ORDERBY" title="ORDER BY Clause">ORDER BY Clause</a>
45       in the <a class="xref" href="sql-select.html" title="SELECT"><span class="refentrytitle">SELECT</span></a> documentation.
46      </p></dd><dt><span class="term"><em class="replaceable"><code>count</code></em></span></dt><dd><p>
47       The maximum number of rows to return.  For details see
48       <a class="xref" href="sql-select.html#SQL-LIMIT" title="LIMIT Clause">LIMIT Clause</a>
49       in the <a class="xref" href="sql-select.html" title="SELECT"><span class="refentrytitle">SELECT</span></a> documentation.
50      </p></dd><dt><span class="term"><em class="replaceable"><code>start</code></em></span></dt><dd><p>
51       The number of rows to skip before starting to return rows.
52       For details see <a class="xref" href="sql-select.html#SQL-LIMIT" title="LIMIT Clause">LIMIT Clause</a>
53       in the <a class="xref" href="sql-select.html" title="SELECT"><span class="refentrytitle">SELECT</span></a> documentation.
54      </p></dd></dl></div></div><div class="refsect1" id="id-1.9.3.185.7"><h2>Notes</h2><p>
55    <code class="command">VALUES</code> lists with very large numbers of rows should be avoided,
56    as you might encounter out-of-memory failures or poor performance.
57    <code class="command">VALUES</code> appearing within <code class="command">INSERT</code> is a special case
58    (because the desired column types are known from the <code class="command">INSERT</code>'s
59    target table, and need not be inferred by scanning the <code class="command">VALUES</code>
60    list), so it can handle larger lists than are practical in other contexts.
61   </p></div><div class="refsect1" id="id-1.9.3.185.8"><h2>Examples</h2><p>
62    A bare <code class="command">VALUES</code> command:
63
64 </p><pre class="programlisting">
65 VALUES (1, 'one'), (2, 'two'), (3, 'three');
66 </pre><p>
67
68    This will return a table of two columns and three rows.  It's effectively
69    equivalent to:
70
71 </p><pre class="programlisting">
72 SELECT 1 AS column1, 'one' AS column2
73 UNION ALL
74 SELECT 2, 'two'
75 UNION ALL
76 SELECT 3, 'three';
77 </pre><p>
78
79   </p><p>
80    More usually, <code class="command">VALUES</code> is used within a larger SQL command.
81    The most common use is in <code class="command">INSERT</code>:
82
83 </p><pre class="programlisting">
84 INSERT INTO films (code, title, did, date_prod, kind)
85     VALUES ('T_601', 'Yojimbo', 106, '1961-06-16', 'Drama');
86 </pre><p>
87   </p><p>
88    In the context of <code class="command">INSERT</code>, entries of a <code class="command">VALUES</code> list
89    can be <code class="literal">DEFAULT</code> to indicate that the column default
90    should be used here instead of specifying a value:
91
92 </p><pre class="programlisting">
93 INSERT INTO films VALUES
94     ('UA502', 'Bananas', 105, DEFAULT, 'Comedy', '82 minutes'),
95     ('T_601', 'Yojimbo', 106, DEFAULT, 'Drama', DEFAULT);
96 </pre><p>
97   </p><p>
98    <code class="command">VALUES</code> can also be used where a sub-<code class="command">SELECT</code> might
99    be written, for example in a <code class="literal">FROM</code> clause:
100
101 </p><pre class="programlisting">
102 SELECT f.*
103   FROM films f, (VALUES('MGM', 'Horror'), ('UA', 'Sci-Fi')) AS t (studio, kind)
104   WHERE f.studio = t.studio AND f.kind = t.kind;
105
106 UPDATE employees SET salary = salary * v.increase
107   FROM (VALUES(1, 200000, 1.2), (2, 400000, 1.4)) AS v (depno, target, increase)
108   WHERE employees.depno = v.depno AND employees.sales &gt;= v.target;
109 </pre><p>
110
111    Note that an <code class="literal">AS</code> clause is required when <code class="command">VALUES</code>
112    is used in a <code class="literal">FROM</code> clause, just as is true for
113    <code class="command">SELECT</code>.  It is not required that the <code class="literal">AS</code> clause
114    specify names for all the columns, but it's good practice to do so.
115    (The default column names for <code class="command">VALUES</code> are <code class="literal">column1</code>,
116    <code class="literal">column2</code>, etc. in <span class="productname">PostgreSQL</span>, but
117    these names might be different in other database systems.)
118   </p><p>
119    When <code class="command">VALUES</code> is used in <code class="command">INSERT</code>, the values are all
120    automatically coerced to the data type of the corresponding destination
121    column.  When it's used in other contexts, it might be necessary to specify
122    the correct data type.  If the entries are all quoted literal constants,
123    coercing the first is sufficient to determine the assumed type for all:
124
125 </p><pre class="programlisting">
126 SELECT * FROM machines
127 WHERE ip_address IN (VALUES('192.168.0.1'::inet), ('192.168.0.10'), ('192.168.1.43'));
128 </pre><div class="tip"><h3 class="title">Tip</h3><p>
129     For simple <code class="literal">IN</code> tests, it's better to rely on the
130     <a class="link" href="functions-comparisons.html#FUNCTIONS-COMPARISONS-IN-SCALAR" title="9.25.1. IN">list-of-scalars</a>
131     form of <code class="literal">IN</code> than to write a <code class="command">VALUES</code>
132     query as shown above.  The list of scalars method requires less writing
133     and is often more efficient.
134    </p></div></div><div class="refsect1" id="id-1.9.3.185.9"><h2>Compatibility</h2><p><code class="command">VALUES</code> conforms to the SQL standard.
135    <code class="literal">LIMIT</code> and <code class="literal">OFFSET</code> are
136    <span class="productname">PostgreSQL</span> extensions; see also
137    under <a class="xref" href="sql-select.html" title="SELECT"><span class="refentrytitle">SELECT</span></a>.
138   </p></div><div class="refsect1" id="id-1.9.3.185.10"><h2>See Also</h2><span class="simplelist"><a class="xref" href="sql-insert.html" title="INSERT"><span class="refentrytitle">INSERT</span></a>, <a class="xref" href="sql-select.html" title="SELECT"><span class="refentrytitle">SELECT</span></a></span></div></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="sql-vacuum.html" title="VACUUM">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="sql-commands.html" title="SQL Commands">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="reference-client.html" title="PostgreSQL Client Applications">Next</a></td></tr><tr><td width="40%" align="left" valign="top">VACUUM </td><td width="20%" align="center"><a accesskey="h" href="index.html" title="PostgreSQL 18.0 Documentation">Home</a></td><td width="40%" align="right" valign="top"> PostgreSQL Client Applications</td></tr></table></div></body></html>