Select

The Select clause is the final projection of a query. It runs once per binding tuple in the current stream and emits one output value per tuple — a scalar, a constructed object, a flat spread of bindings, or an envelope object keyed by source aliases.

Syntax

Railroad

select.*exprselect-listfromsource,sourcewhereexprorderbyorder-key,order-keylimitrange;

BNF

select-stmt ::= "select" select-ctor query-body-opt ";"

select-ctor ::= "."
              | "*"
              | expr
              | select-list

select-list ::= select-item ( "," select-item )*

select-item ::= expr [ "as" identifier ]

query-body-opt ::= ε | query-body

query-body ::= from-clause [ where-clause ] [ order-clause ] [ limit-clause ]

Rules

  1. select expr emits a scalar per row, not an object. (phase: evaluate last — after from, where, order by, and limit)
  2. select item, item, … is shorthand for select {item, item, …}; an item expr as name introduces an output key, and a path item such as t.x uses the last segment as the key.
  3. select * spreads all bindings flat; select . wraps each binding under its source alias.
  4. With no from clause, the query produces exactly one output row.
  5. Aggregate functions are valid only in the select projection (ungrouped aggregation reduces the post-where stream to one row). (phase: evaluate last)

Examples

Minimal

Envelope Object

Select . emits the binding tuple as an envelope object.

SQL

create table T;

insert into T ({"x": 1});

select . from T as t;

Result

[
  { "t": { "x": 1 } }
]

Bindings Flat

Select * spreads bindings flat.

SQL

create table T;

insert into T ({"x": 1, "y": 2});

select * from T as t;

Result

[
  { "x": 1, "y": 2 }
]

Per Row

Select emits a scalar per row.

SQL

create table T;

insert into T ({"x": 1}, {"x": 2});

select t.x from T as t order by t.x;

Result

[
  1,
  2
]

Per Row

Select emits the literal once per row.

SQL

create table T;

insert into T ({"x": 1}, {"x": 2});

select 7 from T as t order by t.x;

Result

[
  7,
  7
]

Per Row

Select emits the object once per row.

SQL

create table T;

insert into T ({"x": 1});

select {"a": t.x} from T as t;

Result

[
  { "a": 1 }
]

Named Field

Select as emits an object with the named field.

SQL

create table T;

insert into T ({"x": 10});

select t.x as a from T as t;

Result

[
  { "a": 10 }
]

List Items

List items may be arbitrary expressions, not only paths.

SQL

create table T;

insert into T ({"x": 1});

select 1 as a, 'hi' as b from T as t;

Result

[
  { "a": 1, "b": "hi" }
]

From

From uses the table name as the implicit alias.

SQL

create table T;

insert into T ({"x": 1});

select T.x from T;

Result

[
  1
]

From

From as binds the source under an explicit alias.

SQL

create table T;

insert into T ({"x": 7});

select t.x from T as t;

Result

[
  7
]

From

From binds the source under an alias without 'as'.

SQL

create table T;

insert into T ({"x": 9});

select t.x from T t;

Result

[
  9
]

An Array

An array literal builds an array from its element expressions.

SQL

create table T;

insert into T ({"x": 7});

select [1, 2, t.x] as a from T as t;

Result

[
  { "a": [ 1, 2, 7 ] }
]

Single Row

Select with no From clause yields the value as a single row.

SQL

create table T;

select 1;

Result

[
  1
]

Compound

Named Member

A list of items emits an object with each named member.

SQL

create table T;

insert into T ({"x": 1, "y": 2});

select t.x as a, t.y as b from T as t;

Result

[
  { "a": 1, "b": 2 }
]

Array Literals

Array literals may nest.

SQL

create table T;

insert into T ({"x": 1});

select [[1, 2], [3]] as a from T as t;

Result

[
  { "a": [ [ 1, 2 ], [ 3 ] ] }
]

Error cases

Nothing Spread

Select * requires a From clause (nothing to spread).

SQL

create table T;

select *;

Expected error: static

Tuple Envelope

Select . requires a From clause (no binding tuple to envelope).

SQL

create table T;

select .;

Expected error: static

See also

  • From — introduces bindings consumed by select
  • Where — filters rows before projection
  • Expressions — constructor and projection expressions