VALUES

VALUES is a table constructor used to define tabular data. It’s utilized with INSERT statements to insert one or more rows.

Syntax

VALUES ( value_expr [, ... ] ) [, ... ]

Usage Notes

value_expr

Each set of comma-separated value_expr in parentheses represents a single row in the result set.

Column Names

Column names of the result table are auto-generated. To rename the column names, add an AS clause.

Aggregations

Aggregations (e.g., SUM, COUNT) cannot be directly used in the VALUES clause.

Examples

Tabular data with VALUES

VALUES (1,2,3,4), (5,6,7,8), (9,10,11,12);

clmn1 |clmn2 |clmn3 |clmn4
------+------+------+-----
1     | 2    | 3    | 4
5     | 6    | 7    | 8
9     | 10   | 11   | 12

Using VALUES in a SELECT Query

SELECT
 t.*
FROM
(
 VALUES
  (1, 2, 3, 'a'),
  (5, 6, 7, 'b'),
  (9, 10, 11, 'c')
) AS t;

clmn1 |clmn2 |clmn3 |clmn4
------+------+------+-----
1     | 2    | 3    | a
5     | 6    | 7    | b
9     | 10   | 11   | c

You can also use this to rename the columns

SELECT
  t.*
FROM
 (
  VALUES
   (1, 2, 3, 'a'),
   (5, 6, 7, 'b'),
   (9, 10, 11, 'c')
 ) AS t(a, b, c, d);

Creating a Table Using VALUES

Use AS to assign names to columns

CREATE TABLE
  cool_animals AS (
   SELECTt.*
   FROM
(
  VALUES
  (1, 'dog'),
  (2, 'cat'),
  (3, 'horse'),
  (4, 'hippopotamus')
)
  AS t(id, name)
  );

Permissions

This clause requires no special permissions.