Basic VALUES syntax in YQL

VALUES as a top-level operator

It lets you create a table from specified values. For example, this statement creates a table of k columns and n rows:

  1. VALUES (expr_11, expr_12, ..., expr_1k),
  2. (expr_21, expr_22, ..., expr_2k),
  3. ....
  4. (expr_n1, expr_n2, ..., expr_nk);

VALUES - 图1

This statement is totally equivalent to the following one:

  1. SELECT expr_11, expr_12, ..., expr_1k UNION ALL
  2. SELECT expr_21, expr_22, ..., expr_2k UNION ALL
  3. ....
  4. SELECT expr_n1, expr_n2, ..., expr_nk;

VALUES - 图2

Example:

  1. VALUES (1,2), (3,4);

VALUES - 图3

VALUES after FROM

VALUES can also be used in a subquery, after FROM. For example, the following two queries are equivalent:

  1. VALUES (1,2), (3,4);
  2. SELECT * FROM (VALUES (1,2), (3,4));

VALUES - 图4

In all the examples above, column names are assigned by YQL and have the format column0 ... columnN. To assign arbitrary column names, you can use the following construct:

  1. SELECT * FROM (VALUES (1,2), (3,4)) as t(x,y);

VALUES - 图5

In this case, the columns will get the names x, y.