1VALUES(7)                PostgreSQL 16.1 Documentation               VALUES(7)
2
3
4

NAME

6       VALUES - compute a set of rows
7

SYNOPSIS

9       VALUES ( expression [, ...] ) [, ...]
10           [ ORDER BY sort_expression [ ASC | DESC | USING operator ] [, ...] ]
11           [ LIMIT { count | ALL } ]
12           [ OFFSET start [ ROW | ROWS ] ]
13           [ FETCH { FIRST | NEXT } [ count ] { ROW | ROWS } ONLY ]
14

DESCRIPTION

16       VALUES computes a row value or set of row values specified by value
17       expressions. It is most commonly used to generate a “constant table”
18       within a larger command, but it can be used on its own.
19
20       When more than one row is specified, all the rows must have the same
21       number of elements. The data types of the resulting table's columns are
22       determined by combining the explicit or inferred types of the
23       expressions appearing in that column, using the same rules as for UNION
24       (see Section 10.5).
25
26       Within larger commands, VALUES is syntactically allowed anywhere that
27       SELECT is. Because it is treated like a SELECT by the grammar, it is
28       possible to use the ORDER BY, LIMIT (or equivalently FETCH FIRST), and
29       OFFSET clauses with a VALUES command.
30

PARAMETERS

32       expression
33           A constant or expression to compute and insert at the indicated
34           place in the resulting table (set of rows). In a VALUES list
35           appearing at the top level of an INSERT, an expression can be
36           replaced by DEFAULT to indicate that the destination column's
37           default value should be inserted.  DEFAULT cannot be used when
38           VALUES appears in other contexts.
39
40       sort_expression
41           An expression or integer constant indicating how to sort the result
42           rows. This expression can refer to the columns of the VALUES result
43           as column1, column2, etc. For more details see ORDER BY Clause in
44           the SELECT(7) documentation.
45
46       operator
47           A sorting operator. For details see ORDER BY Clause in the
48           SELECT(7) documentation.
49
50       count
51           The maximum number of rows to return. For details see LIMIT Clause
52           in the SELECT(7) documentation.
53
54       start
55           The number of rows to skip before starting to return rows. For
56           details see LIMIT Clause in the SELECT(7) documentation.
57

NOTES

59       VALUES lists with very large numbers of rows should be avoided, as you
60       might encounter out-of-memory failures or poor performance.  VALUES
61       appearing within INSERT is a special case (because the desired column
62       types are known from the INSERT's target table, and need not be
63       inferred by scanning the VALUES list), so it can handle larger lists
64       than are practical in other contexts.
65

EXAMPLES

67       A bare VALUES command:
68
69           VALUES (1, 'one'), (2, 'two'), (3, 'three');
70
71       This will return a table of two columns and three rows. It's
72       effectively equivalent to:
73
74           SELECT 1 AS column1, 'one' AS column2
75           UNION ALL
76           SELECT 2, 'two'
77           UNION ALL
78           SELECT 3, 'three';
79
80       More usually, VALUES is used within a larger SQL command. The most
81       common use is in INSERT:
82
83           INSERT INTO films (code, title, did, date_prod, kind)
84               VALUES ('T_601', 'Yojimbo', 106, '1961-06-16', 'Drama');
85
86       In the context of INSERT, entries of a VALUES list can be DEFAULT to
87       indicate that the column default should be used here instead of
88       specifying a value:
89
90           INSERT INTO films VALUES
91               ('UA502', 'Bananas', 105, DEFAULT, 'Comedy', '82 minutes'),
92               ('T_601', 'Yojimbo', 106, DEFAULT, 'Drama', DEFAULT);
93
94       VALUES can also be used where a sub-SELECT might be written, for
95       example in a FROM clause:
96
97           SELECT f.*
98             FROM films f, (VALUES('MGM', 'Horror'), ('UA', 'Sci-Fi')) AS t (studio, kind)
99             WHERE f.studio = t.studio AND f.kind = t.kind;
100
101           UPDATE employees SET salary = salary * v.increase
102             FROM (VALUES(1, 200000, 1.2), (2, 400000, 1.4)) AS v (depno, target, increase)
103             WHERE employees.depno = v.depno AND employees.sales >= v.target;
104
105       Note that an AS clause is required when VALUES is used in a FROM
106       clause, just as is true for SELECT. It is not required that the AS
107       clause specify names for all the columns, but it's good practice to do
108       so. (The default column names for VALUES are column1, column2, etc. in
109       PostgreSQL, but these names might be different in other database
110       systems.)
111
112       When VALUES is used in INSERT, the values are all automatically coerced
113       to the data type of the corresponding destination column. When it's
114       used in other contexts, it might be necessary to specify the correct
115       data type. If the entries are all quoted literal constants, coercing
116       the first is sufficient to determine the assumed type for all:
117
118           SELECT * FROM machines
119           WHERE ip_address IN (VALUES('192.168.0.1'::inet), ('192.168.0.10'), ('192.168.1.43'));
120
121           Tip
122           For simple IN tests, it's better to rely on the list-of-scalars
123           form of IN than to write a VALUES query as shown above. The list of
124           scalars method requires less writing and is often more efficient.
125

COMPATIBILITY

127       VALUES conforms to the SQL standard.  LIMIT and OFFSET are PostgreSQL
128       extensions; see also under SELECT(7).
129

SEE ALSO

131       INSERT(7), SELECT(7)
132
133
134
135PostgreSQL 16.1                      2023                            VALUES(7)
Impressum