BigQuery reference
BigQuery SQL statements and clauses
How to write the queries and statements around the functions: CTEs, joins, QUALIFY and PIVOT; tables, partitions and clusters; INSERT, UPDATE and MERGE; scripts; and the INFORMATION_SCHEMA views that tell you what all of it costs.
Nothing matches. Try the functions or the error messages.
Query syntax
- 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.
DDL statements
- ALTER TABLEALTER TABLE changes a BigQuery table without rewriting it: ADD COLUMN, DROP COLUMN, RENAME TO, RENAME COLUMN, SET OPTIONS (expiration, description, labels, partition filter) and ALTER COLUMN SET DATA TYPE for widening changes such as INT64 to NUMERIC.
- CLUSTER BYCLUSTER BY sorts a BigQuery table's storage blocks by up to four columns, so queries that filter on those columns skip blocks and bill fewer bytes.
- CREATE FUNCTIONCREATE FUNCTION defines a user-defined function (UDF) in SQL or JavaScript.
- CREATE MATERIALIZED VIEWCREATE MATERIALIZED VIEW stores the result of an aggregation over a BigQuery table and keeps it current automatically.
- CREATE TABLECREATE TABLE creates a BigQuery table from a column list, from a query (CREATE TABLE AS SELECT), or both, with optional partitioning, clustering and OPTIONS such as expiration and description.
- CREATE TEMP TABLECREATE TEMP TABLE stores intermediate results in a table that exists only for the current multi-statement query or session, without a dataset.
- CREATE VIEWCREATE VIEW saves a query under a table-like name.
- PARTITION BY (partitioned tables)PARTITION BY in CREATE TABLE splits a BigQuery table into partitions by a DATE, TIMESTAMP or DATETIME column, by ingestion time, or by integer ranges.
DML statements
- DELETEDELETE removes the rows of a BigQuery table that match a WHERE clause, which is mandatory.
- INSERTINSERT adds rows to a BigQuery table from a VALUES list or a SELECT query.
- MERGEMERGE updates, inserts and deletes rows of a BigQuery table in one atomic statement, by matching it against a source table or query.
- TRUNCATE TABLETRUNCATE TABLE removes every row from a BigQuery table but keeps its schema, partitioning, clustering, description and labels.
- UPDATEUPDATE changes column values in existing BigQuery rows.
Scripting and data movement
- DECLARE and SETDECLARE creates a variable in a BigQuery multi-statement query and SET assigns it: DECLARE run_date DATE DEFAULT CURRENT_DATE(); SET run_date = '2026-11-22'.
- EXECUTE IMMEDIATEEXECUTE IMMEDIATE runs a SQL statement built as a string at run time, with values bound through USING and results stored with INTO.
- EXPORT DATAEXPORT DATA writes the result of a query to files in Cloud Storage (CSV, JSON, Avro or Parquet), or to Bigtable, Spanner, Pub/Sub or AlloyDB.
- LOAD DATALOAD DATA loads files from Cloud Storage (CSV, JSON, Avro, Parquet, ORC) into a BigQuery table from SQL: LOAD DATA INTO appends, LOAD DATA OVERWRITE replaces, and a missing table is created.
- LOOP, WHILE and FOR...INBigQuery scripts loop with LOOP ...
INFORMATION_SCHEMA
- INFORMATION_SCHEMAINFORMATION_SCHEMA is BigQuery's set of read-only metadata views for tables, columns, partitions, storage and jobs.
- INFORMATION_SCHEMA.JOBSINFORMATION_SCHEMA.JOBS lists every BigQuery job in a project for the last 180 days, with bytes billed, slot milliseconds, errors and query text.
