Query statements scan one or more tables or expressions and return the computed result rows. This topic describes the syntax for SQL queries in GoogleSQL for Spanner.
SQL syntax notation rules
The following table lists and describes the syntax notation rules that GoogleSQL documentation commonly uses.
| Notation | Example | Description |
|---|---|---|
| Square brackets | [ ] |
Optional clauses |
| Parentheses | ( ) |
Literal parentheses |
| Vertical bar | | |
Logical XOR (exclusive OR) |
| Curly braces | { } |
A set of options, such as { a | b | c }. Select one option. |
| Ellipsis | ... |
The preceding item can repeat. |
| Comma | , |
Literal comma |
| Comma followed by an ellipsis | , ... |
The preceding item can repeat in a comma-separated list. |
| Item list | item [, ...] |
One or more items |
[item, ...] |
Zero or more items | |
| Double quotes | "" |
The enclosed syntax characters (for example, "{"..."}") are literal
and required. |
| Angle brackets | <> |
Literal angle brackets |
SQL syntax
query_statement: [ statement_hint_expr ] [ table_hint_expr ] [ group_hint_expr ] [ join_hint_expr ] query_expr query_expr: [ WITH cte[, ...] ] { select | ( query_expr ) | set_operation } [ ORDER BY expression [{ ASC | DESC }] [, ...] ] [ LIMIT count [ OFFSET skip_rows ] ] [ FOR UPDATE ] select: SELECT [ { ALL | DISTINCT } ] [ AS { typename | STRUCT | VALUE } ] select_list [ FROM from_clause[, ...] ] [ WHERE bool_expression ] [ GROUP [ group_hint_expr ] BY group_by_specification ] [ HAVING bool_expression ]
SELECT statement
SELECT
[ { ALL | DISTINCT } ]
[ AS { typename | STRUCT | VALUE } ]
select_list
select_list:
{ select_all | select_expression } [, ...]
select_all:
[ expression. ]*
[ EXCEPT ( column_name [, ...] ) ]
[ REPLACE ( expression AS column_name [, ...] ) ]
select_expression:
expression [ [ AS ] alias ]
The SELECT list defines the columns that the query will return. Expressions in
the SELECT list can refer to columns in any of the from_items in its
corresponding FROM clause.
Each item in the SELECT list is one of:
*expressionexpression.*
SELECT *
SELECT *, often referred to as select star, produces one output column for
each column that's visible after executing the full query.
SELECT * FROM (SELECT "apple" AS fruit, "carrot" AS vegetable);
/*-------+-----------+
| fruit | vegetable |
+-------+-----------+
| apple | carrot |
+-------+-----------*/
SELECT expression
Items in a SELECT list can be expressions. These expressions evaluate to a
single value and produce one output column, with an optional explicit alias.
If the expression doesn't have an explicit alias, it receives an implicit alias according to the rules for implicit aliases, if possible. Otherwise, the column is anonymous and you can't refer to it by name elsewhere in the query.
SELECT expression.*
An item in a SELECT list can also take the form of expression.*. This
produces one output column for each column or top-level field of expression.
The expression must either be a table alias or evaluate to a single value of a
data type with fields, such as a STRUCT.
The following query produces one output column for each column in the table
groceries, aliased as g.
WITH groceries AS
(SELECT "milk" AS dairy,
"eggs" AS protein,
"bread" AS grain)
SELECT g.*
FROM groceries AS g;
/*-------+---------+-------+
| dairy | protein | grain |
+-------+---------+-------+
| milk | eggs | bread |
+-------+---------+-------*/
More examples:
WITH locations AS
(SELECT STRUCT("Seattle" AS city, "Washington" AS state) AS location
UNION ALL
SELECT STRUCT("Phoenix" AS city, "Arizona" AS state) AS location)
SELECT l.location.*
FROM locations l;
/*---------+------------+
| city | state |
+---------+------------+
| Seattle | Washington |
| Phoenix | Arizona |
+---------+------------*/
WITH locations AS
(SELECT ARRAY<STRUCT<city STRING, state STRING>>[("Seattle", "Washington"),
("Phoenix", "Arizona")] AS location)
SELECT l.LOCATION[offset(0)].*
FROM locations l;
/*---------+------------+
| city | state |
+---------+------------+
| Seattle | Washington |
+---------+------------*/
SELECT * EXCEPT
A SELECT * EXCEPT statement specifies the names of one or more columns to
exclude from the result. All matching column names are omitted from the output.
WITH orders AS
(SELECT 5 as order_id,
"sprocket" as item_name,
200 as quantity)
SELECT * EXCEPT (order_id)
FROM orders;
/*-----------+----------+
| item_name | quantity |
+-----------+----------+
| sprocket | 200 |
+-----------+----------*/
SELECT * REPLACE
A SELECT * REPLACE statement specifies one or more
expression AS identifier clauses. Each identifier must match a column name
from the SELECT * statement. In the output column list, the column that
matches the identifier in a REPLACE clause is replaced by the expression in
that REPLACE clause.
A SELECT * REPLACE statement doesn't change the names or order of columns.
However, it can change the value and the value type.
WITH orders AS
(SELECT 5 as order_id,
"sprocket" as item_name,