3 PostgreSQL documentation
6 <refentry id=
"SQL-VALUES">
8 <refentrytitle id=
"SQL-VALUES-TITLE">VALUES
</refentrytitle>
9 <manvolnum>7</manvolnum>
10 <refmiscinfo>SQL - Language Statements
</refmiscinfo>
14 <refname>VALUES
</refname>
15 <refpurpose>compute a set of rows
</refpurpose>
18 <indexterm zone=
"sql-values">
19 <primary>VALUES
</primary>
24 VALUES (
<replaceable class=
"PARAMETER">expression
</replaceable> [, ...] ) [, ...]
25 [ ORDER BY
<replaceable class=
"parameter">sort_expression
</replaceable> [ ASC | DESC | USING
<replaceable class=
"parameter">operator
</replaceable> ] [, ...] ]
26 [ LIMIT {
<replaceable class=
"parameter">count
</replaceable> | ALL } ]
27 [ OFFSET
<replaceable class=
"parameter">start
</replaceable> [ ROW | ROWS ] ]
28 [ FETCH { FIRST | NEXT } [
<replaceable class=
"parameter">count
</replaceable> ] { ROW | ROWS } ONLY ]
33 <title>Description
</title>
36 <command>VALUES
</command> computes a row value or set of row values
37 specified by value expressions. It is most commonly used to generate
38 a
<quote>constant table<
/> within a larger command, but it can be
43 When more than one row is specified, all the rows must have the same
44 number of elements. The data types of the resulting table's columns are
45 determined by combining the explicit or inferred types of the expressions
46 appearing in that column, using the same rules as for
<literal>UNION<
/>
47 (see
<xref linkend=
"typeconv-union-case">).
51 Within larger commands,
<command>VALUES<
/> is syntactically allowed
52 anywhere that
<command>SELECT<
/> is. Because it is treated like a
53 <command>SELECT<
/> by the grammar, it is possible to use
54 the
<literal>ORDER BY<
/>,
<literal>LIMIT<
/> (or
55 equivalently
<literal>FETCH FIRST
</literal>),
56 and
<literal>OFFSET<
/> clauses with a
57 <command>VALUES<
/> command.
62 <title>Parameters
</title>
66 <term><replaceable class=
"PARAMETER">expression
</replaceable></term>
69 A constant or expression to compute and insert at the indicated place
70 in the resulting table (set of rows). In a
<command>VALUES<
/> list
71 appearing at the top level of an
<command>INSERT<
/>, an
72 <replaceable class=
"PARAMETER">expression
</replaceable> can be replaced
73 by
<literal>DEFAULT
</literal> to indicate that the destination column's
74 default value should be inserted.
<literal>DEFAULT
</literal> cannot
75 be used when
<command>VALUES<
/> appears in other contexts.
81 <term><replaceable class=
"parameter">sort_expression
</replaceable></term>
84 An expression or integer constant indicating how to sort the result
85 rows. This expression can refer to the columns of the
86 <command>VALUES<
/> result as
<literal>column1<
/>,
<literal>column2<
/>,
87 etc. For more details see
88 <xref linkend=
"sql-orderby" endterm=
"sql-orderby-title">.
94 <term><replaceable class=
"parameter">operator
</replaceable></term>
97 A sorting operator. For details see
98 <xref linkend=
"sql-orderby" endterm=
"sql-orderby-title">.
104 <term><replaceable class=
"parameter">count
</replaceable></term>
107 The maximum number of rows to return. For details see
108 <xref linkend=
"sql-limit" endterm=
"sql-limit-title">.
114 <term><replaceable class=
"parameter">start
</replaceable></term>
117 The number of rows to skip before starting to return rows.
119 <xref linkend=
"sql-limit" endterm=
"sql-limit-title">.
130 <command>VALUES<
/> lists with very large numbers of rows should be avoided,
131 as you might encounter out-of-memory failures or poor performance.
132 <command>VALUES<
/> appearing within
<command>INSERT<
/> is a special case
133 (because the desired column types are known from the
<command>INSERT<
/>'s
134 target table, and need not be inferred by scanning the
<command>VALUES<
/>
135 list), so it can handle larger lists than are practical in other contexts.
140 <title>Examples
</title>
143 A bare
<command>VALUES<
/> command:
146 VALUES (
1, 'one'), (
2, 'two'), (
3, 'three');
149 This will return a table of two columns and three rows. It's effectively
153 SELECT
1 AS column1, 'one' AS column2
163 More usually,
<command>VALUES<
/> is used within a larger SQL command.
164 The most common use is in
<command>INSERT<
/>:
167 INSERT INTO films (code, title, did, date_prod, kind)
168 VALUES ('T_601', 'Yojimbo',
106, '
1961-
06-
16', 'Drama');
173 In the context of
<command>INSERT<
/>, entries of a
<command>VALUES<
/> list
174 can be
<literal>DEFAULT
</literal> to indicate that the column default
175 should be used here instead of specifying a value:
178 INSERT INTO films VALUES
179 ('UA502', 'Bananas',
105, DEFAULT, 'Comedy', '
82 minutes'),
180 ('T_601', 'Yojimbo',
106, DEFAULT, 'Drama', DEFAULT);
185 <command>VALUES<
/> can also be used where a sub-
<command>SELECT<
/> might
186 be written, for example in a
<literal>FROM<
/> clause:
190 FROM films f, (VALUES('MGM', 'Horror'), ('UA', 'Sci-Fi')) AS t (studio, kind)
191 WHERE f.studio = t.studio AND f.kind = t.kind;
193 UPDATE employees SET salary = salary * v.increase
194 FROM (VALUES(
1,
200000,
1.2), (
2,
400000,
1.4)) AS v (depno, target, increase)
195 WHERE employees.depno = v.depno AND employees.sales
>= v.target;
198 Note that an
<literal>AS<
/> clause is required when
<command>VALUES<
/>
199 is used in a
<literal>FROM<
/> clause, just as is true for
200 <command>SELECT<
/>. It is not required that the
<literal>AS<
/> clause
201 specify names for all the columns, but it's good practice to do so.
202 (The default column names for
<command>VALUES<
/> are
<literal>column1<
/>,
203 <literal>column2<
/>, etc in
<productname>PostgreSQL
</productname>, but
204 these names might be different in other database systems.)
208 When
<command>VALUES<
/> is used in
<command>INSERT<
/>, the values are all
209 automatically coerced to the data type of the corresponding destination
210 column. When it's used in other contexts, it might be necessary to specify
211 the correct data type. If the entries are all quoted literal constants,
212 coercing the first is sufficient to determine the assumed type for all:
215 SELECT * FROM machines
216 WHERE ip_address IN (VALUES('
192.168.0.1'::inet), ('
192.168.0.10'), ('
192.168.1.43'));
222 For simple
<literal>IN<
/> tests, it's better to rely on the
223 list-of-scalars form of
<literal>IN<
/> than to write a
<command>VALUES<
/>
224 query as shown above. The list of scalars method requires less writing
225 and is often more efficient.
231 <title>Compatibility
</title>
234 <command>VALUES
</command> conforms to the SQL standard.
235 <literal>LIMIT
</literal> and
<literal>OFFSET
</literal> are
236 <productname>PostgreSQL
</productname> extensions; see also
237 under
<xref linkend=
"sql-select" endterm=
"sql-select-title">.
242 <title>See Also
</title>
244 <simplelist type=
"inline">
245 <member><xref linkend=
"sql-insert" endterm=
"sql-insert-title"></member>
246 <member><xref linkend=
"sql-select" endterm=
"sql-select-title"></member>