3 .\" Author: The PostgreSQL Global Development Group
4 .\" Generator: DocBook XSL Stylesheets vsnapshot <http://docbook.sf.net/>
6 .\" Manual: PostgreSQL 18.0 Documentation
7 .\" Source: PostgreSQL 18.0
10 .TH "VALUES" "7" "2025" "PostgreSQL 18.0" "PostgreSQL 18.0 Documentation"
11 .\" -----------------------------------------------------------------
12 .\" * Define some portability stuff
13 .\" -----------------------------------------------------------------
14 .\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
15 .\" http://bugs.debian.org/507673
16 .\" http://lists.gnu.org/archive/html/groff/2009-02/msg00013.html
17 .\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
20 .\" -----------------------------------------------------------------
21 .\" * set default formatting
22 .\" -----------------------------------------------------------------
23 .\" disable hyphenation
25 .\" disable justification (adjust text to left margin only)
27 .\" -----------------------------------------------------------------
28 .\" * MAIN CONTENT STARTS HERE *
29 .\" -----------------------------------------------------------------
31 VALUES \- compute a set of rows
35 VALUES ( \fIexpression\fR [, \&.\&.\&.] ) [, \&.\&.\&.]
36 [ ORDER BY \fIsort_expression\fR [ ASC | DESC | USING \fIoperator\fR ] [, \&.\&.\&.] ]
37 [ LIMIT { \fIcount\fR | ALL } ]
38 [ OFFSET \fIstart\fR [ ROW | ROWS ] ]
39 [ FETCH { FIRST | NEXT } [ \fIcount\fR ] { ROW | ROWS } ONLY ]
44 computes a row value or set of row values specified by value expressions\&. It is most commonly used to generate a
45 \(lqconstant table\(rq
46 within a larger command, but it can be used on its own\&.
48 When more than one row is specified, all the rows must have the same number of elements\&. The data types of the resulting table\*(Aqs columns are determined by combining the explicit or inferred types of the expressions appearing in that column, using the same rules as for
53 Within larger commands,
55 is syntactically allowed anywhere that
57 is\&. Because it is treated like a
59 by the grammar, it is possible to use the
72 A constant or expression to compute and insert at the indicated place in the resulting table (set of rows)\&. In a
74 list appearing at the top level of an
79 to indicate that the destination column\*(Aqs default value should be inserted\&.
83 appears in other contexts\&.
88 An expression or integer constant indicating how to sort the result rows\&. This expression can refer to the columns of the
92 column2, etc\&. For more details see
101 A sorting operator\&. For details see
110 The maximum number of rows to return\&. For details see
119 The number of rows to skip before starting to return rows\&. For details see
128 lists with very large numbers of rows should be avoided, as you might encounter out\-of\-memory failures or poor performance\&.
132 is a special case (because the desired column types are known from the
133 \fBINSERT\fR\*(Aqs target table, and need not be inferred by scanning the
135 list), so it can handle larger lists than are practical in other contexts\&.
146 VALUES (1, \*(Aqone\*(Aq), (2, \*(Aqtwo\*(Aq), (3, \*(Aqthree\*(Aq);
152 This will return a table of two columns and three rows\&. It\*(Aqs effectively equivalent to:
158 SELECT 1 AS column1, \*(Aqone\*(Aq AS column2
160 SELECT 2, \*(Aqtwo\*(Aq
162 SELECT 3, \*(Aqthree\*(Aq;
170 is used within a larger SQL command\&. The most common use is in
177 INSERT INTO films (code, title, did, date_prod, kind)
178 VALUES (\*(AqT_601\*(Aq, \*(AqYojimbo\*(Aq, 106, \*(Aq1961\-06\-16\*(Aq, \*(AqDrama\*(Aq);
185 \fBINSERT\fR, entries of a
189 to indicate that the column default should be used here instead of specifying a value:
195 INSERT INTO films VALUES
196 (\*(AqUA502\*(Aq, \*(AqBananas\*(Aq, 105, DEFAULT, \*(AqComedy\*(Aq, \*(Aq82 minutes\*(Aq),
197 (\*(AqT_601\*(Aq, \*(AqYojimbo\*(Aq, 106, DEFAULT, \*(AqDrama\*(Aq, DEFAULT);
204 can also be used where a sub\-\fBSELECT\fR
205 might be written, for example in a
214 FROM films f, (VALUES(\*(AqMGM\*(Aq, \*(AqHorror\*(Aq), (\*(AqUA\*(Aq, \*(AqSci\-Fi\*(Aq)) AS t (studio, kind)
215 WHERE f\&.studio = t\&.studio AND f\&.kind = t\&.kind;
217 UPDATE employees SET salary = salary * v\&.increase
218 FROM (VALUES(1, 200000, 1\&.2), (2, 400000, 1\&.4)) AS v (depno, target, increase)
219 WHERE employees\&.depno = v\&.depno AND employees\&.sales >= v\&.target;
227 clause is required when
231 clause, just as is true for
232 \fBSELECT\fR\&. It is not required that the
234 clause specify names for all the columns, but it\*(Aqs good practice to do so\&. (The default column names for
239 PostgreSQL, but these names might be different in other database systems\&.)
244 \fBINSERT\fR, the values are all automatically coerced to the data type of the corresponding destination column\&. When it\*(Aqs used in other contexts, it might be necessary to specify the correct data type\&. If the entries are all quoted literal constants, coercing the first is sufficient to determine the assumed type for all:
250 SELECT * FROM machines
251 WHERE ip_address IN (VALUES(\*(Aq192\&.168\&.0\&.1\*(Aq::inet), (\*(Aq192\&.168\&.0\&.10\*(Aq), (\*(Aq192\&.168\&.1\&.43\*(Aq));
261 .nr an-no-space-flag 1
271 tests, it\*(Aqs better to rely on the
277 query as shown above\&. The list of scalars method requires less writing and is often more efficient\&.
283 conforms to the SQL standard\&.
289 extensions; see also under
292 \fBINSERT\fR(7), \fBSELECT\fR(7)