# BigQuery SQL Syntax: Statements, Clauses, DDL and DML | Masthead

BigQuery SQL syntax reference: QUALIFY, PIVOT, MERGE, CTEs, partitioned and clustered tables, scripting and INFORMATION_SCHEMA. 31 statements and clauses with examples and cost notes.

> For the index of this section of the site, see [llms.txt](https://mastheadata.com/llms.txt)

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](https://mastheadata.com/bigquery/functions.md) or the [error messages](https://mastheadata.com/bigquery/errors.md).

## [Query syntax](https://mastheadata.com/bigquery/query-syntax.md)

*   [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.](https://mastheadata.com/bigquery/sql/group-by-rollup.md)
*   [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.](https://mastheadata.com/bigquery/sql/join.md)
*   [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.](https://mastheadata.com/bigquery/sql/pivot.md)
*   [QUALIFYQUALIFY filters rows on the result of a window function, the way HAVING filters on aggregates.](https://mastheadata.com/bigquery/sql/qualify.md)
*   [SELECT \ EXCEPTSELECT EXCEPT (col1, col2) returns every column except the ones you list.](https://mastheadata.com/bigquery/sql/select-except.md)
*   [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.](https://mastheadata.com/bigquery/sql/select-replace.md)
*   [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.](https://mastheadata.com/bigquery/sql/tablesample.md)
*   [UNION ALL and UNION DISTINCTUNION ALL stacks the rows of two queries; UNION DISTINCT does the same and removes duplicates.](https://mastheadata.com/bigquery/sql/union.md)
*   [UNPIVOTUNPIVOT turns columns into rows: UNPIVOT(amount FOR month IN (jan, feb, mar)) gives one row per month with its value in amount.](https://mastheadata.com/bigquery/sql/unpivot.md)
*   [WITH (CTE)WITH defines named subqueries (common table expressions) that the rest of a query can reference like tables.](https://mastheadata.com/bigquery/sql/with.md)
*   [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.](https://mastheadata.com/bigquery/sql/with-recursive.md)

## [DDL statements](https://mastheadata.com/bigquery/ddl.md)

*   [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.](https://mastheadata.com/bigquery/sql/alter-table.md)
*   [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.](https://mastheadata.com/bigquery/sql/cluster-by.md)
*   [CREATE FUNCTIONCREATE FUNCTION defines a user-defined function (UDF) in SQL or JavaScript.](https://mastheadata.com/bigquery/sql/create-function.md)
*   [CREATE MATERIALIZED VIEWCREATE MATERIALIZED VIEW stores the result of an aggregation over a BigQuery table and keeps it current automatically.](https://mastheadata.com/bigquery/sql/create-materialized-view.md)
*   [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.](https://mastheadata.com/bigquery/sql/create-table.md)
*   [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.](https://mastheadata.com/bigquery/sql/create-temp-table.md)
*   [CREATE VIEWCREATE VIEW saves a query under a table-like name.](https://mastheadata.com/bigquery/sql/create-view.md)
*   [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.](https://mastheadata.com/bigquery/sql/partition-by.md)

## [DML statements](https://mastheadata.com/bigquery/dml.md)

*   [DELETEDELETE removes the rows of a BigQuery table that match a WHERE clause, which is mandatory.](https://mastheadata.com/bigquery/sql/delete.md)
*   [INSERTINSERT adds rows to a BigQuery table from a VALUES list or a SELECT query.](https://mastheadata.com/bigquery/sql/insert.md)
*   [MERGEMERGE updates, inserts and deletes rows of a BigQuery table in one atomic statement, by matching it against a source table or query.](https://mastheadata.com/bigquery/sql/merge.md)
*   [TRUNCATE TABLETRUNCATE TABLE removes every row from a BigQuery table but keeps its schema, partitioning, clustering, description and labels.](https://mastheadata.com/bigquery/sql/truncate-table.md)
*   [UPDATEUPDATE changes column values in existing BigQuery rows.](https://mastheadata.com/bigquery/sql/update.md)

## [Scripting and data movement](https://mastheadata.com/bigquery/scripting.md)

*   [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'.](https://mastheadata.com/bigquery/sql/declare-set.md)
*   [EXECUTE IMMEDIATEEXECUTE IMMEDIATE runs a SQL statement built as a string at run time, with values bound through USING and results stored with INTO.](https://mastheadata.com/bigquery/sql/execute-immediate.md)
*   [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.](https://mastheadata.com/bigquery/sql/export-data.md)
*   [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.](https://mastheadata.com/bigquery/sql/load-data.md)
*   [LOOP, WHILE and FOR...INBigQuery scripts loop with LOOP ...](https://mastheadata.com/bigquery/sql/loops.md)

## [INFORMATION\SCHEMA](https://mastheadata.com/bigquery/information-schema-views.md)

*   [INFORMATION\SCHEMAINFORMATION\SCHEMA is BigQuery's set of read-only metadata views for tables, columns, partitions, storage and jobs.](https://mastheadata.com/bigquery/sql/information-schema.md)
*   [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.](https://mastheadata.com/bigquery/sql/information-schema-jobs.md)
---

## Other pages in BigQuery Reference

> For the full index of this section, see [BigQuery Reference](https://mastheadata.com/llms/llms-bigquery-reference.txt).

- [BigQuery Aggregate Functions: COUNT, SUM, STRING_AGG and More | Masthead](https://mastheadata.com/bigquery/aggregate-functions.md): Aggregate functions collapse many rows into one value per group. 16 aggregate functions with syntax, runnable examples, common mistakes and cost notes.
- [BigQuery Array Functions: UNNEST, ARRAY_AGG and More | Masthead](https://mastheadata.com/bigquery/array-functions.md): ARRAY is a first-class type in BigQuery: nested and repeated fields are how BigQuery avoids joins. 11 array functions with syntax, runnable examples, common mistakes and cost notes.
- [BigQuery Conditional Expressions: CASE, IF, COALESCE, IFNULL | Masthead](https://mastheadata.com/bigquery/conditional-expressions.md): Conditional expressions choose a value based on a condition or on NULLs. 5 conditional expressions with syntax, runnable examples, common mistakes and cost notes.
- [BigQuery Conversion Functions: CAST and SAFE_CAST | Masthead](https://mastheadata.com/bigquery/conversion-functions.md): Conversion functions change a value from one type to another. 3 conversion functions with syntax, runnable examples, common mistakes and cost notes.
- [BigQuery Cost Queries: SQL to Find and Cut BigQuery Spend | Masthead](https://mastheadata.com/bigquery/cost-queries.md): 20 ready-to-run SQL queries for BigQuery cost: the most expensive queries, cost by user, project, label, dbt model and dashboard, slot usage, storage cost, unused and dead-end tables.
- [BigQuery Date Functions: Syntax, Examples and Gotchas | Masthead](https://mastheadata.com/bigquery/date-functions.md): DATE functions in BigQuery (GoogleSQL) build, shift, truncate, compare and format calendar dates. 14 date functions with syntax, runnable examples, common mistakes and cost notes.
- [BigQuery Datetime Functions: Syntax and Examples | Masthead](https://mastheadata.com/bigquery/datetime-functions.md): DATETIME is a civil date and time with no time zone: what a wall clock shows. 8 datetime functions with syntax, runnable examples, common mistakes and cost notes.
- [BigQuery DDL: CREATE TABLE, Partitioning, Clustering and Views | Masthead](https://mastheadata.com/bigquery/ddl.md): Data definition language creates and changes tables, views, materialized views and functions. 8 DDL statements with syntax, runnable examples, common mistakes and cost notes.
- [BigQuery DML: INSERT, UPDATE, DELETE and MERGE | Masthead](https://mastheadata.com/bigquery/dml.md): Data manipulation language changes table rows. 5 DML statements with syntax, runnable examples, common mistakes and cost notes.
- [BigQuery Error Messages: Causes and Fixes | Masthead](https://mastheadata.com/bigquery/errors.md): 51 BigQuery error messages explained: what each one means, why it happens and how to fix it, from "Resources exceeded" and quota errors to permissions, DML conflicts and failed loads.
- [BigQuery Functions List: Syntax and Examples for Every Function | Masthead](https://mastheadata.com/bigquery/functions.md): All 143 BigQuery SQL functions we cover, by category: date, timestamp, string, array, JSON, aggregate, window and more. Each with syntax, runnable examples, common mistakes and cost notes.
- [GA4 BigQuery Export: SQL Queries for Sessions, Users, Events and Revenue | Masthead](https://mastheadata.com/bigquery/ga4.md): 28 SQL recipes for the GA4 BigQuery export: unnest event_params, sessions, users, traffic sources, engagement, ecommerce, retention, and how to keep the queries cheap. Every one runs on Google's public GA4 sample.
- [BigQuery INFORMATION_SCHEMA: Jobs, Tables, Storage and Costs | Masthead](https://mastheadata.com/bigquery/information-schema-views.md): INFORMATION_SCHEMA views expose BigQuery metadata as tables you can query: jobs, slots, bytes billed, tables, columns and storage. 2 INFORMATION_SCHEMA with syntax, runnable examples, common mistakes and cost notes.
- [BigQuery JSON Functions: JSON_VALUE, JSON_QUERY and More | Masthead](https://mastheadata.com/bigquery/json-functions.md): BigQuery reads JSON stored as STRING or as the native JSON type. 14 JSON functions with syntax, runnable examples, common mistakes and cost notes.
- [BigQuery Math Functions: ROUND, SAFE_DIVIDE, MOD and More | Masthead](https://mastheadata.com/bigquery/math-functions.md): Mathematical functions round, divide, bucket and transform numbers. 15 mathematical functions with syntax, runnable examples, common mistakes and cost notes.
- [BigQuery Pricing Calculator: On-Demand vs Editions and Storage | Masthead](https://mastheadata.com/bigquery/pricing-calculator.md): Free BigQuery pricing calculator: compare on-demand ($6.25 per TiB) with Standard, Enterprise and Enterprise Plus editions per slot-hour, and logical vs physical storage billing, using your own numbers from INFORMATION_SCHEMA.
- [BigQuery Pricing Explained (2026): On-Demand, Editions and Storage | Masthead](https://mastheadata.com/bigquery/pricing.md): BigQuery bills compute and storage separately. Compute is on-demand at $6.25 per TiB scanned (the first 1 TiB a month is free) or capacity-based with editions from $0.04 per slot-hour. Storage costs $0.02 per GiB a month for active data and $0.01 for data unchanged for 90 days (US, logical).
- [BigQuery Public Datasets: Sizes, Tables and What They Cost to Query | Masthead](https://mastheadata.com/bigquery/public-datasets.md): 40 BigQuery public datasets with their real table sizes and what querying them costs: GitHub, Stack Overflow, Wikipedia, Google Trends, GA4 sample, NOAA weather, taxi trips, crypto and more, with cheap example queries.
- [BigQuery Query Syntax: QUALIFY, PIVOT, CTEs, JOINs and More | Masthead](https://mastheadata.com/bigquery/query-syntax.md): The clauses that shape a GoogleSQL query in BigQuery: CTEs, joins, set operations, PIVOT, QUALIFY and SELECT * EXCEPT. 11 query syntax with syntax, runnable examples, common mistakes and cost notes.
- [BigQuery Scripting: Variables, Loops, Dynamic SQL, LOAD and EXPORT | Masthead](https://mastheadata.com/bigquery/scripting.md): Multi-statement queries let you declare variables, loop, branch and build SQL at run time, and LOAD DATA and EXPORT DATA move data in and out of BigQuery from SQL.. 5 scripting and data movement with syntax, runnable examples, common mistakes and cost notes.
- [BigQuery String Functions: Syntax and Examples | Masthead](https://mastheadata.com/bigquery/string-functions.md): String functions in BigQuery split, search, replace, pad and format STRING values, and most also accept BYTES. 28 string functions with syntax, runnable examples, common mistakes and cost notes.
- [The Bill Is Right: Guess Your BigQuery Bill | Masthead](https://mastheadata.com/bigquery/the-bill-is-right.md): A free game show for data teams. Guess the monthly BigQuery bill from real Google Cloud list prices: three tries, land within 15% to win.
- [BigQuery Timestamp Functions: Syntax, Time Zones and Examples | Masthead](https://mastheadata.com/bigquery/timestamp-functions.md): TIMESTAMP is an absolute point in time with microsecond precision, stored in UTC. 12 timestamp functions with syntax, runnable examples, common mistakes and cost notes.
- [BigQuery Hash and Utility Functions: FARM_FINGERPRINT, UUID, SHA256 | Masthead](https://mastheadata.com/bigquery/utility-functions.md): Hashing, unique IDs and a few utility functions that show up in pipelines: deterministic sampling, surrogate keys, deduplication and raising your own errors.. 6 hash and utility functions with syntax, runnable examples, common mistakes and cost notes.
- [BigQuery Window Functions: ROW_NUMBER, LAG, RANK and More | Masthead](https://mastheadata.com/bigquery/window-functions.md): Window (analytic) functions compute a value for every row from a window of related rows, defined with OVER (PARTITION BY ... 11 window functions with syntax, runnable examples, common mistakes and cost notes.
