A newer version of this documentation is available.

View Latest

SELECT statement syntax

select:

subselect [ set-op ([ALL] )  subselect]* [order-by-clause] [limit-clause]  [offset-clause]

set-op:

UNION | INTERSECT | EXCEPT

subselect:

select-from | from-select

select-from:

select-clause [from-clause] [let-clause] [where-clause] [group-by-clause]

from-select:

from-clause [let-clause] [where-clause] [group-by-clause] select-clause

SELECT Clause

select-clause:

SELECT ( [ALL | DISTINCT]  ( result-expression [, result-expression]*) |
    (RAW | ELEMENT) expression)

result-expression:

[(path .)] * | expression [ ([AS] alias) ]

path:

identifier [[ expression ]]  [. path]

alias:

identifier

FROM clause

from-clause:

FROM from-term

from-term:

from-path [([AS] alias)] [use-keys-clause] | from-term join-clause | from-term nest-clause | from-term unnest-clause

from-path:

[(namespace :)] path

namespace:

identifier

use-keys-clause:

USE [PRIMARY] KEYS expression

join-clause:

[join-type] JOIN from-path [( [AS] alias) ] on-keys-clause

join-type:

INNER | LEFT [OUTER]

on-keys-clause:

ON [PRIMARY] KEYS expression

nest-clause:

[join-type] NEST from-path [ ( [AS] alias) ] on-keys-clause

unnest-clause:

[join-type] [UNNEST | FLATTEN] expression [ ( [AS] alias) ]

LET clause

LET (alias = expression) [, (alias = expression)]*

WHERE clause

where-clause:

WHERE condition

cond:

expression

GROUP BY clause

group-by-clause:

GROUP BY expression [ , expression ]* [ letting-clause ]
    [having-clause] ] | [letting-clause]

letting-clause:

LETTING  alias = expression [(, alias = expression) ]*

having-clause:

HAVING condition

ORDER BY clause

order-by-clause:

ORDER BY ordering-term [, ordering-term]*

ordering-term:

expression [ASC | DESC]

LIMIT clause

LIMIT expression

OFFSET clause

OFFSET expression