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 clause | What it does |
|---|---|
| GROUP BY ROLLUP, CUBE and GROUPING SETS | ROLLUP, 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. |
| JOIN | JOIN 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. |
| PIVOT | PIVOT turns row values into columns with an aggregate: PIVOT(SUM(revenue) FOR quarter IN ('Q1', 'Q2', 'Q3', 'Q4')) gives one revenue column per quarter. |
| QUALIFY | QUALIFY filters rows on the result of a window function, the way HAVING filters on aggregates. |
| SELECT * EXCEPT | SELECT EXCEPT (col1, col2) returns every column except the ones you list. |
| SELECT * REPLACE | SELECT REPLACE (expression AS column) returns every column but swaps the value of the named columns for an expression, keeping their names and positions. |
| TABLESAMPLE SYSTEM | TABLESAMPLE 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 DISTINCT | UNION ALL stacks the rows of two queries; UNION DISTINCT does the same and removes duplicates. |
| UNPIVOT | UNPIVOT 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 RECURSIVE | WITH 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
Date functionsDatetime functionsTimestamp functionsString functionsAggregate functionsWindow functionsArray functionsJSON functionsConversion functionsConditional expressionsMathematical functionsHash and utility functionsDDL statementsDML statementsScripting and data movementINFORMATION_SCHEMAError messagesBigQuery pricingPricing calculator
