`VALUES` Lists
VALUES Lists
VALUES provides a way to generate a “constant table” that can be used in a query without having to actually create and populate a table on-disk. The syntax is
VALUES ( EXPRESSION [, ...] ) [, ...]
UNION (see UNION, CASE, and Related Constructs).
As an example:
VALUES (1, 'one'), (2, 'two'), (3, 'three');
SELECT 1 AS column1, 'one' AS column2
UNION ALL
SELECT 2, 'two'
UNION ALL
SELECT 3, 'three';
column1, column2, etc. to the columns of a VALUES table. The column names are not specified by the SQL standard and different database systems do it differently, so it's usually better to override the default names with a table alias list, like this:
=> SELECT * FROM (VALUES (1, 'one'), (2, 'two'), (3, 'three')) AS t (num,letter);
num | letter
-----+--------
1 | one
2 | two
3 | three
(3 rows)
Syntactically, VALUES followed by expression lists is treated as equivalent to:
SELECT SELECT_LIST FROM TABLE_EXPRESSION
SELECT can. For example, you can use it as part of a UNION, or attach a sort_specification (ORDER BY, LIMIT, and/or OFFSET) to it. VALUES is most commonly used as the data source in an INSERT command, and next most commonly as a subquery.
For more information see sql-values.