]> begriffs open source - ai-pg/blob - full-docs/txt/queries-values.txt
Convert HTML docs to more streamlined TXT
[ai-pg] / full-docs / txt / queries-values.txt
1
2 7.7. VALUES Lists #
3
4    VALUES provides a way to generate a “constant table” that can be used
5    in a query without having to actually create and populate a table
6    on-disk. The syntax is
7 VALUES ( expression [, ...] ) [, ...]
8
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
13    column of the result is determined using the same rules as for UNION
14    (see Section 10.5).
15
16    As an example:
17 VALUES (1, 'one'), (2, 'two'), (3, 'three');
18
19    will return a table of two columns and three rows. It's effectively
20    equivalent to:
21 SELECT 1 AS column1, 'one' AS column2
22 UNION ALL
23 SELECT 2, 'two'
24 UNION ALL
25 SELECT 3, 'three';
26
27    By default, PostgreSQL assigns the names column1, column2, etc. to the
28    columns of a VALUES table. The column names are not specified by the
29    SQL standard and different database systems do it differently, so it's
30    usually better to override the default names with a table alias list,
31    like this:
32 => SELECT * FROM (VALUES (1, 'one'), (2, 'two'), (3, 'three')) AS t (num,letter)
33 ;
34  num | letter
35 -----+--------
36    1 | one
37    2 | two
38    3 | three
39 (3 rows)
40
41    Syntactically, VALUES followed by expression lists is treated as
42    equivalent to:
43 SELECT select_list FROM table_expression
44
45    and can appear anywhere a SELECT can. For example, you can use it as
46    part of a UNION, or attach a sort_specification (ORDER BY, LIMIT,
47    and/or OFFSET) to it. VALUES is most commonly used as the data source
48    in an INSERT command, and next most commonly as a subquery.
49
50    For more information see VALUES.