Request a demo

BigQuery reference

BigQuery query syntax

The clauses that shape a GoogleSQL query in BigQuery: CTEs, joins, set operations, PIVOT, QUALIFY and SELECT * EXCEPT. Most of them change what you scan, so they change what you pay.

Statements and clauses in this category

Statement or clauseWhat it does
GROUP BY ROLLUP, CUBE and GROUPING SETSROLLUP, CUBE and GROUPING SETS compute several GROUP BY levels in one query: ROLLUP(a, b) adds subtotals per a and a grand total, CUBE(a, b) adds every combination, and GROUPING SETS lists the exact groupings you want.
JOINJOIN combines rows of two tables on a condition: INNER JOIN keeps matches, LEFT, RIGHT and FULL OUTER JOIN also keep unmatched rows with NULLs, and CROSS JOIN pairs every row with every row.
PIVOTPIVOT turns row values into columns with an aggregate: PIVOT(SUM(revenue) FOR quarter IN ('Q1', 'Q2', 'Q3', 'Q4')) gives one revenue column per quarter.
QUALIFYQUALIFY filters rows on the result of a window function, the way HAVING filters on aggregates.
SELECT * EXCEPTSELECT EXCEPT (col1, col2) returns every column except the ones you list.
SELECT * REPLACESELECT REPLACE (expression AS column) returns every column but swaps the value of the named columns for an expression, keeping their names and positions.
TABLESAMPLE SYSTEMTABLESAMPLE SYSTEM (n PERCENT) reads a random subset of a BigQuery table's storage blocks instead of the whole table, so exploratory queries on large tables scan and bill only about n percent of the bytes.
UNION ALL and UNION DISTINCTUNION ALL stacks the rows of two queries; UNION DISTINCT does the same and removes duplicates.
UNPIVOTUNPIVOT turns columns into rows: UNPIVOT(amount FOR month IN (jan, feb, mar)) gives one row per month with its value in amount.
WITH (CTE)WITH defines named subqueries (common table expressions) that the rest of a query can reference like tables.
WITH RECURSIVEWITH RECURSIVE defines a CTE that references itself, so a query can walk hierarchies and graphs of unknown depth: org charts, category trees, dependency lineage.

More of the BigQuery reference

Observability for every BigQuery table, in about 15 minutes

Masthead detects freshness, volume and schema anomalies, monitors pipeline errors and maps column-level lineage across all your projects, from logs and metadata only.