]> begriffs open source - ai-pg/blob - full-docs/src/sgml/html/queries-values.html
PG 18 docs from https://ftp.postgresql.org/pub/source/v18.0/postgresql-18.0-docs...
[ai-pg] / full-docs / src / sgml / html / queries-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>7.7. VALUES Lists</title><link rel="stylesheet" type="text/css" href="stylesheet.css" /><link rev="made" href="pgsql-docs@lists.postgresql.org" /><meta name="generator" content="DocBook XSL Stylesheets Vsnapshot" /><link rel="prev" href="queries-limit.html" title="7.6. LIMIT and OFFSET" /><link rel="next" href="queries-with.html" title="7.8. WITH Queries (Common Table Expressions)" /></head><body id="docContent" class="container-fluid col-10"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="5" align="center">7.7. <code class="literal">VALUES</code> Lists</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="queries-limit.html" title="7.6. LIMIT and OFFSET">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="queries.html" title="Chapter 7. Queries">Up</a></td><th width="60%" align="center">Chapter 7. Queries</th><td width="10%" align="right"><a accesskey="h" href="index.html" title="PostgreSQL 18.0 Documentation">Home</a></td><td width="10%" align="right"> <a accesskey="n" href="queries-with.html" title="7.8. WITH Queries (Common Table Expressions)">Next</a></td></tr></table><hr /></div><div class="sect1" id="QUERIES-VALUES"><div class="titlepage"><div><div><h2 class="title" style="clear: both">7.7. <code class="literal">VALUES</code> Lists <a href="#QUERIES-VALUES" class="id_link">#</a></h2></div></div></div><a id="id-1.5.6.11.2" class="indexterm"></a><p>
3    <code class="literal">VALUES</code> provides a way to generate a <span class="quote">“<span class="quote">constant table</span>”</span>
4    that can be used in a query without having to actually create and populate
5    a table on-disk.  The syntax is
6 </p><pre class="synopsis">
7 VALUES ( <em class="replaceable"><code>expression</code></em> [, ...] ) [, ...]
8 </pre><p>
9    Each parenthesized list of expressions generates a row in the table.
10    The lists must all have the same number of elements (i.e., the number
11    of columns in the table), and corresponding entries in each list must
12    have compatible data types.  The actual data type assigned to each column
13    of the result is determined using the same rules as for <code class="literal">UNION</code>
14    (see <a class="xref" href="typeconv-union-case.html" title="10.5. UNION, CASE, and Related Constructs">Section 10.5</a>).
15   </p><p>
16    As an example:
17 </p><pre class="programlisting">
18 VALUES (1, 'one'), (2, 'two'), (3, 'three');
19 </pre><p>
20
21    will return a table of two columns and three rows.  It's effectively
22    equivalent to:
23 </p><pre class="programlisting">
24 SELECT 1 AS column1, 'one' AS column2
25 UNION ALL
26 SELECT 2, 'two'
27 UNION ALL
28 SELECT 3, 'three';
29 </pre><p>
30
31    By default, <span class="productname">PostgreSQL</span> assigns the names
32    <code class="literal">column1</code>, <code class="literal">column2</code>, etc. to the columns of a
33    <code class="literal">VALUES</code> table.  The column names are not specified by the
34    SQL standard and different database systems do it differently, so
35    it's usually better to override the default names with a table alias
36    list, like this:
37 </p><pre class="programlisting">
38 =&gt; SELECT * FROM (VALUES (1, 'one'), (2, 'two'), (3, 'three')) AS t (num,letter);
39  num | letter
40 -----+--------
41    1 | one
42    2 | two
43    3 | three
44 (3 rows)
45 </pre><p>
46   </p><p>
47    Syntactically, <code class="literal">VALUES</code> followed by expression lists is
48    treated as equivalent to:
49 </p><pre class="synopsis">
50 SELECT <em class="replaceable"><code>select_list</code></em> FROM <em class="replaceable"><code>table_expression</code></em>
51 </pre><p>
52    and can appear anywhere a <code class="literal">SELECT</code> can.  For example, you can
53    use it as part of a <code class="literal">UNION</code>, or attach a
54    <em class="replaceable"><code>sort_specification</code></em> (<code class="literal">ORDER BY</code>,
55    <code class="literal">LIMIT</code>, and/or <code class="literal">OFFSET</code>) to it.  <code class="literal">VALUES</code>
56    is most commonly used as the data source in an <code class="command">INSERT</code> command,
57    and next most commonly as a subquery.
58   </p><p>
59    For more information see <a class="xref" href="sql-values.html" title="VALUES"><span class="refentrytitle">VALUES</span></a>.
60   </p></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="queries-limit.html" title="7.6. LIMIT and OFFSET">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="queries.html" title="Chapter 7. Queries">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="queries-with.html" title="7.8. WITH Queries (Common Table Expressions)">Next</a></td></tr><tr><td width="40%" align="left" valign="top">7.6. <code class="literal">LIMIT</code> and <code class="literal">OFFSET</code> </td><td width="20%" align="center"><a accesskey="h" href="index.html" title="PostgreSQL 18.0 Documentation">Home</a></td><td width="40%" align="right" valign="top"> 7.8. <code class="literal">WITH</code> Queries (Common Table Expressions)</td></tr></table></div></body></html>