Practical SQL

System variables in BigQuery

Constantin LunguUpdated 1 min read

Photo by Alison Ivansek on Unsplash

Today's quick post is about system variables in BigQuery. What are they?

They are a special type of variables, available in scripts (multi-statement queries). You can use them to read (and sometimes write) query metadata during query execution.

Here's a couple of examples:
- @@query_label - read/write query labels, making easier for you to group your queries based on they are used for
- @@time_zone - read/write default time zone to use
- @@script.job_id - the job id of the current job

Check out below an example with slot_ms (returns slot time in millis), bytes_billed and creation_date.

SELECT ds_date,
       COUNT(DISTINCT value) AS count_values

FROM `learning.data_source`

GROUP BY ds_date;

SELECT
  @@project_id AS project_id,
  @@script.slot_ms AS script_slot_ms,
  @@script.bytes_billed AS script_bytes_billed,
  @@script.creation_time AS script_creation_time

BigQuery results: script_slot_ms 636839, script_bytes_billed 10485760 and script_creation_time 2024-06-22 16:07:41.402000 (the UTC suffix is cut off); the project_id value is hidden.