BigQuery adds WITH expressions for temporary variables in GoogleSQL queries

August 12, 2025 at 2:44:06 AM

TL;DR BigQuery now supports WITH expressions in GoogleSQL to create temporary variables within queries. Variables are assigned once and can be used in subsequent expressions but not as arguments in aggregate or analytic functions. Each variable is evaluated once. This feature differs from WITH clauses and allows for cleaner, modular queries with examples showing concatenation, random value evaluation, and aggregate calculations.

BigQuery adds WITH expressions for temporary variables in GoogleSQL queries

The BigQuery update introduces the general availability (GA) of WITH expressions in GoogleSQL queries, allowing the creation of temporary variables within a query. This feature enables defining one or more variables that can be referenced in subsequent expressions inside the same WITH expression, ultimately returning the value of a final result expression.

Syntax and Usage

The syntax is:

WITH(variable_assignment[, ...], result_expression)
  • variable_assignment: Defines a variable as variable_name AS expression. Each variable name must be unique within the WITH expression.
  • result_expression: Uses all previously defined variables and its value is returned by the WITH expression.

Variables can reference those defined before them but can only be assigned once. Each variable's expression is evaluated only once.

Important Rules and Limitations

  • Variables created in WITH expressions cannot be used as arguments in analytic or aggregate functions (e.g., WITH(a AS ..., SUM(a)) causes an error).
  • Variables can store results of aggregate or analytic functions but cannot be passed as inputs to such functions.
  • WITH expressions differ from WITH clauses, which define temporary tables.

Examples

  1. Concatenation of variables:
SELECT WITH(
  a AS '123',
  b AS CONCAT(a, '456'),
  c AS '789',
  CONCAT(b, c)
) AS result;

Result: '123456789'

  1. Volatile expression evaluated once:
SELECT WITH(a AS RAND(), a - a);

Result: 0.0

  1. Storing aggregate results in variables:
SELECT WITH(
  s AS SUM(input),
  c AS COUNT(input),
  s/c
)
FROM UNNEST([1.0, 2.0, 3.0]) AS input;

Result: 2.0

  1. Error when using variables as aggregate function arguments:
SELECT WITH(diff AS a - b, AVG(diff))
FROM UNNEST([
  STRUCT(1 AS a, 2 AS b),
  STRUCT(3 AS a, 4 AS b),
  STRUCT(5 AS a, 6 AS b)
]);

This produces an error because variables like diff cannot be used inside aggregate or analytic function arguments.

  1. Using WITH expression alongside WITH clause:
WITH my_table AS (
  SELECT 1 AS x, 2 AS y
  UNION ALL
  SELECT 3 AS x, 4 AS y
  UNION ALL
  SELECT 5 AS x, 6 AS y
)
SELECT WITH(
  a AS SUM(x),
  b AS COUNT(x),
  a/b
) AS avg_x, AVG(y) AS avg_y
FROM my_table
WHERE x > 1;

Result:

avg_x avg_y
4 5

This example shows how WITH expressions can be combined with WITH clauses to compute temporary variables and aggregate values in a single query.


The introduction of WITH expressions in BigQuery enhances query flexibility by enabling temporary variable creation and reuse within queries, while maintaining clear restrictions to avoid misuse in aggregate or analytic function arguments.

Have more questions on this topic? Ask our AI assistant for in-depth insights.

Read more from sources 👇

The Only Digital Marketing Feed You'll Ever Need.

Stay informed your way. Tailored updates when and how you want them. 100% Free.

10,000+ Users

500+ Sources

1000+ Tools

Or

Related Posts

BigQuery Update Enabling advanced runtime with short query optimizations in preview

BigQuery Update Enabling advanced runtime with short query optimizations in preview

Google Cloud
Google Cloud

Official Source

Official Source

Google Cloud is a Official Source. The source has been verified by Swipe Insight team.

Official Source
BigQuery Introduces New Vector Index Management and Data Loading Features

BigQuery Introduces New Vector Index Management and Data Loading Features

Google Cloud
Google Cloud

Official Source

Official Source

Google Cloud is a Official Source. The source has been verified by Swipe Insight team.

Official Source
BigQuery launches DISTINCT pipe operator for selecting unique rows in queries

BigQuery launches DISTINCT pipe operator for selecting unique rows in queries

Google Cloud
Google Cloud

Official Source

Official Source

Google Cloud is a Official Source. The source has been verified by Swipe Insight team.

Official Source
Automate Meta Ads Creative Generation and Uploading

Automate Meta Ads Creative Generation and Uploading

Featured
Markifact
Markifact

Verified Sponsor

Verified Sponsor

Markifact is a Verified Sponsor. Want to get featured here? Contact us.

Verified Sponsor
BigQuery sets default on-demand query limit to 200TB per day from September 2025

BigQuery sets default on-demand query limit to 200TB per day from September 2025

BigQuery Sharing Listings Can Now Be Monetized on Google Cloud Marketplace

BigQuery Sharing Listings Can Now Be Monetized on Google Cloud Marketplace

Google Cloud
Google Cloud

Official Source

Official Source

Google Cloud is a Official Source. The source has been verified by Swipe Insight team.

Official Source
Google Introduces Automated Data Insights Feature for BigQuery with Gemini Integration

Google Introduces Automated Data Insights Feature for BigQuery with Gemini Integration

Google Cloud
Google Cloud

Official Source

Official Source

Google Cloud is a Official Source. The source has been verified by Swipe Insight team.

Official Source
BigQuery adds configuration settings and new data loading features

BigQuery adds configuration settings and new data loading features

Google Cloud
Google Cloud

Official Source

Official Source

Google Cloud is a Official Source. The source has been verified by Swipe Insight team.

Official Source

Related Tools

Markifact logo

Markifact

Verified Tool

Verified Tool

Markifact is a Verified Tool. Want to get this badge? Contact us.

Verified Tool

Marketing Workflows Powered by AI

Featured
Marketing Auditor logo

Marketing Auditor

Verified Tool

Verified Tool

Marketing Auditor is a Verified Tool. Want to get this badge? Contact us.

Verified Tool

Automated audits for Google Ads and Analytics.

Get Featured Here

Showcase your tool in this list.

Contact Us
Databricks logo

Databricks

Generative AI-powered data intelligence platform

Data Engineering
GA4 SQL logo

GA4 SQL

Verified Tool

Verified Tool

GA4 SQL is a Verified Tool. Want to get this badge? Contact us.

Verified Tool

Generate GA4 BigQuery queries easily

Data Analysis
TapClicks logo

TapClicks

Automated marketing solutions powered by your data

Data Engineering
Stitch logo

Stitch

Automated cloud data pipelines, no coding needed

Data Engineering
Akkio logo

Akkio

AI-powered analytics for agencies

Data Analysis
NinjaCat logo

NinjaCat

AI-powered marketing data and analytics platform

Reporting
Funnel logo

Funnel

Aggregate and analyze marketing data seamlessly

Reporting
Fivetran logo

Fivetran

Effortlessly centralize and move data from any source

Data Engineering

Get Featured Here

Showcase your tool in this list.

Contact Us