4 VALUES — compute a set of rows
8 VALUES ( expression [, ...] ) [, ...]
9 [ ORDER BY sort_expression [ ASC | DESC | USING operator ] [, ...] ]
10 [ LIMIT { count | ALL } ]
11 [ OFFSET start [ ROW | ROWS ] ]
12 [ FETCH { FIRST | NEXT } [ count ] { ROW | ROWS } ONLY ]
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.
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
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.
34 A constant or expression to compute and insert at the indicated
35 place in the resulting table (set of rows). In a VALUES list
36 appearing at the top level of an INSERT, an expression can be
37 replaced by DEFAULT to indicate that the destination column's
38 default value should be inserted. DEFAULT cannot be used when
39 VALUES appears in other contexts.
42 An expression or integer constant indicating how to sort the
43 result rows. This expression can refer to the columns of the
44 VALUES result as column1, column2, etc. For more details see
45 ORDER BY Clause in the SELECT documentation.
48 A sorting operator. For details see ORDER BY Clause in the
52 The maximum number of rows to return. For details see LIMIT
53 Clause in the SELECT documentation.
56 The number of rows to skip before starting to return rows. For
57 details see LIMIT Clause in the SELECT documentation.
61 VALUES lists with very large numbers of rows should be avoided, as you
62 might encounter out-of-memory failures or poor performance. VALUES
63 appearing within INSERT is a special case (because the desired column
64 types are known from the INSERT's target table, and need not be
65 inferred by scanning the VALUES list), so it can handle larger lists
66 than are practical in other contexts.
70 A bare VALUES command:
71 VALUES (1, 'one'), (2, 'two'), (3, 'three');
73 This will return a table of two columns and three rows. It's
74 effectively equivalent to:
75 SELECT 1 AS column1, 'one' AS column2
81 More usually, VALUES is used within a larger SQL command. The most
82 common use is in INSERT:
83 INSERT INTO films (code, title, did, date_prod, kind)
84 VALUES ('T_601', 'Yojimbo', 106, '1961-06-16', 'Drama');
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
89 INSERT INTO films VALUES
90 ('UA502', 'Bananas', 105, DEFAULT, 'Comedy', '82 minutes'),
91 ('T_601', 'Yojimbo', 106, DEFAULT, 'Drama', DEFAULT);
93 VALUES can also be used where a sub-SELECT might be written, for
94 example in a FROM clause:
96 FROM films f, (VALUES('MGM', 'Horror'), ('UA', 'Sci-Fi')) AS t (studio, kind)
97 WHERE f.studio = t.studio AND f.kind = t.kind;
99 UPDATE employees SET salary = salary * v.increase
100 FROM (VALUES(1, 200000, 1.2), (2, 400000, 1.4)) AS v (depno, target, increase)
101 WHERE employees.depno = v.depno AND employees.sales >= v.target;
103 Note that an AS clause is required when VALUES is used in a FROM
104 clause, just as is true for SELECT. It is not required that the AS
105 clause specify names for all the columns, but it's good practice to do
106 so. (The default column names for VALUES are column1, column2, etc. in
107 PostgreSQL, but these names might be different in other database
110 When VALUES is used in INSERT, the values are all automatically coerced
111 to the data type of the corresponding destination column. When it's
112 used in other contexts, it might be necessary to specify the correct
113 data type. If the entries are all quoted literal constants, coercing
114 the first is sufficient to determine the assumed type for all:
115 SELECT * FROM machines
116 WHERE ip_address IN (VALUES('192.168.0.1'::inet), ('192.168.0.10'), ('192.168.1.
121 For simple IN tests, it's better to rely on the list-of-scalars form of
122 IN than to write a VALUES query as shown above. The list of scalars
123 method requires less writing and is often more efficient.
127 VALUES conforms to the SQL standard. LIMIT and OFFSET are PostgreSQL
128 extensions; see also under SELECT.