Run interactive and batch query jobs
This document shows you how to run two types of query jobs in BigQuery:
- Interactive query jobs, which are jobs that BigQuery runs on demand.
- Batch query jobs, which are jobs that BigQuery waits to run until idle compute resources are available.
Interactive versus batch queries
By default, BigQuery runs your queries as interactive query jobs, which are run as soon as possible. Interactive queries count toward your concurrent limit.
With batch query jobs, BigQuery queues your queries and starts them when idle resources are available in the BigQuery shared resource pool. Typically, queries are queued for only a few minutes.
Batch queries don't count toward your concurrent limit. You can run a maximum of 10 concurrent batch queries in your project. Batch queries use the same resources as interactive queries. If you use flat-rate pricing, batch queries and interactive queries share your allocated slots.
BigQuery saves query results to either a temporary table (default) or permanent table. When you specify a permanent table as the destination table for the results, you can choose whether to append or overwrite an existing table, or create a new table with a unique name.
Required roles
To get the permissions that you need to run a query job, ask your administrator to grant you the following IAM roles:
-
BigQuery Job User (
roles/bigquery.jobUser
) on the project. -
BigQuery Data Viewer (
roles/bigquery.dataViewer
) on all tables and views that your query references. To query views, you also need this role on all underlying tables and views. If you're using authorized views or authorized datasets, you don't need access to the underlying source data.
For more information about granting roles, see Manage access.
These predefined roles contain the permissions required to run a query job. To see the exact permissions that are required, expand the Required permissions section:
Required permissions
The following permissions are required to run a query job:
-
bigquery.jobs.create
on the project. -
bigquery.tables.getData
on all tables and views that your query references. To query views, you also need this permission on all underlying tables and views. If you're using authorized views or authorized datasets, you don't need access to the underlying source data.
You might also be able to get these permissions with custom roles or other predefined roles.
For more information about BigQuery permissions, see Access control with IAM.
Run an interactive query
To run an interactive query, select one of the following options:
Console
Go to the BigQuery page.
Click
Compose new query.In the query editor, enter a valid GoogleSQL query.
For example, query the BigQuery public dataset
usa_names
to determine the most common names in the United States between the years 1910 and 2013:SELECT name, gender, SUM(number) AS total FROM `bigquery-public-data.usa_names.usa_1910_2013` GROUP BY name, gender ORDER BY total DESC LIMIT 10;
Optional: Specify the destination table and location for the query results:
- In the query editor, click More, and then click Query settings.
- In the Destination section, select Set a destination table for query results.
- For Dataset, enter the name of an existing dataset for the
destination table—for example,
myProject.myDataset
. - For Table Id, enter a name for the destination table—for
example,
myTable
. If the destination table is an existing table, then for Destination table write preference, select whether to append or overwrite the table with the query results.
If the destination table is a new table, then BigQuery creates the table when you run your query.
In the Additional settings section, click the Data location menu, and then select an option.
In this example, the
usa_names
dataset is stored in the US multi-region location. If you specify a destination table for this query, the dataset that contains the destination table must also be in the US multi-region. You cannot query a dataset in one location and write the results to a table in another location.Click Save.
Click
Run.If you don't specify a destination table, the query job writes the output to a temporary (cache) table.
bq
-
In the Google Cloud console, activate Cloud Shell.
At the bottom of the Google Cloud console, a Cloud Shell session starts and displays a command-line prompt. Cloud Shell is a shell environment with the Google Cloud CLI already installed and with values already set for your current project. It can take a few seconds for the session to initialize.
Use the
bq query
command. In the following example, the--use_legacy_sql=false
flag lets you use GoogleSQL syntax.bq query \ --use_legacy_sql=false \ 'QUERY'
Replace QUERY with a valid GoogleSQL query. For example, query the BigQuery public dataset
usa_names
to determine the most common names in the United States between the years 1910 and 2013:bq query \ --use_legacy_sql=false \ 'SELECT name, gender, SUM(number) AS total FROM `bigquery-public-data.usa_names.usa_1910_2013` GROUP BY name, gender ORDER BY total DESC LIMIT 10;'
The query job writes the output to a temporary (cache) table.
Optionally, you can specify the destination table and location for the query results. To write the results to an existing table, include the appropriate flag to append (
--append_table=true
) or overwrite (--replace=true
) the table.bq query \ --location=LOCATION \ --destination_table=TABLE \ --use_legacy_sql=false \ 'QUERY'
Replace the following:
LOCATION: the region or multi-region for the destination table—for example,
US
In this example, the
usa_names
dataset is stored in the US multi-region location. If you specify a destination table for this query, the dataset that contains the destination table must also be in the US multi-region. You cannot query a dataset in one location and write the results to a table in another location.You can set a default value for the location using the .bigqueryrc file.
TABLE: a name for the destination table—for example,
myDataset.myTable
If the destination table is a new table, then BigQuery creates the table when you run your query. However, you must specify an existing dataset.
If the table isn't in your current project, then add the Google Cloud project ID using the format
PROJECT_ID:DATASET.TABLE
—for example,myProject:myDataset.myTable
. If--destination_table
is unspecified, a query job is generated that writes the output to a temporary table.
API
To run a query using the API, insert a new job
and populate the query
job configuration property. Optionally specify your
location in the location
property in the jobReference
section of the
job resource.
Poll for results by calling
getQueryResults
.
Poll until jobComplete
equals true
. Check for errors and warnings in the
errors
list.
C#
Before trying this sample, follow the C# setup instructions in the BigQuery quickstart using client libraries. For more information, see the BigQuery C# API reference documentation.
Go
Before trying this sample, follow the Go setup instructions in the BigQuery quickstart using client libraries. For more information, see the BigQuery Go API reference documentation.
Java
Before trying this sample, follow the Java setup instructions in the BigQuery quickstart using client libraries. For more information, see the BigQuery Java API reference documentation.
To run a query with a proxy, see Configuring a proxy.
Node.js
Before trying this sample, follow the Node.js setup instructions in the BigQuery quickstart using client libraries. For more information, see the BigQuery Node.js API reference documentation.
PHP
Before trying this sample, follow the PHP setup instructions in the BigQuery quickstart using client libraries. For more information, see the BigQuery PHP API reference documentation.
Python
Before trying this sample, follow the Python setup instructions in the BigQuery quickstart using client libraries. For more information, see the BigQuery Python API reference documentation.
Ruby
Before trying this sample, follow the Ruby setup instructions in the BigQuery quickstart using client libraries. For more information, see the BigQuery Ruby API reference documentation.
For more information, see Interactive versus batch queries.
Run a batch query
To run a batch query, select one of the following options:
Console
Go to the BigQuery page.
Click
Compose new query.In the query editor, enter a valid GoogleSQL query.
For example, query the BigQuery public dataset
usa_names
to determine the most common names in the United States between the years 1910 and 2013:SELECT name, gender, SUM(number) AS total FROM `bigquery-public-data.usa_names.usa_1910_2013` GROUP BY name, gender ORDER BY total DESC LIMIT 10;
Click
More, and then click Query settings.In the Resource management section, select Batch.
Optional: Specify the destination table and location for the query results:
- In the Destination section, select Set a destination table for query results.
- For Dataset, enter the name of an existing dataset for the
destination table—for example,
myProject.myDataset
. - For Table Id, enter a name for the destination table—for
example,
myTable
. If the destination table is an existing table, then for Destination table write preference, select whether to append or overwrite the table with the query results.
If the destination table is a new table, then BigQuery creates the table when you run your query.
In the Additional settings section, click the Data location menu, and then select an option.
In this example, the
usa_names
dataset is stored in the US multi-region location. If you specify a destination table for this query, the dataset that contains the destination table must also be in the US multi-region. You cannot query a dataset in one location and write the results to a table in another location.
Click Save.
Click
Run.If you don't specify a destination table, the query job writes the output to a temporary (cache) table.
bq
-
In the Google Cloud console, activate Cloud Shell.
At the bottom of the Google Cloud console, a Cloud Shell session starts and displays a command-line prompt. Cloud Shell is a shell environment with the Google Cloud CLI already installed and with values already set for your current project. It can take a few seconds for the session to initialize.
Use the
bq query
command and specify the--batch
flag. In the following example, the--use_legacy_sql=false
flag lets you use GoogleSQL syntax.bq query \ --batch \ --use_legacy_sql=false \ 'QUERY'
Replace QUERY with a valid GoogleSQL query. For example, query the BigQuery public dataset
usa_names
to determine the most common names in the United States between the years 1910 and 2013:bq query \ --batch \ --use_legacy_sql=false \ 'SELECT name, gender, SUM(number) AS total FROM `bigquery-public-data.usa_names.usa_1910_2013` GROUP BY name, gender ORDER BY total DESC LIMIT 10;'
The query job writes the output to a temporary (cache) table.
Optionally, you can specify the destination table and location for the query results. To write the results to an existing table, include the appropriate flag to append (
--append_table=true
) or overwrite (--replace=true
) the table.bq query \ --batch \ --location=LOCATION \ --destination_table=TABLE \ --use_legacy_sql=false \ 'QUERY'
Replace the following:
LOCATION: the region or multi-region for the destination table—for example,
US
In this example, the
usa_names
dataset is stored in the US multi-region location. If you specify a destination table for this query, the dataset that contains the destination table must also be in the US multi-region. You cannot query a dataset in one location and write the results to a table in another location.You can set a default value for the location using the .bigqueryrc file.
TABLE: a name for the destination table—for example,
myDataset.myTable
If the destination table is a new table, then BigQuery creates the table when you run your query. However, you must specify an existing dataset.
If the table isn't in your current project, then add the Google Cloud project ID using the format
PROJECT_ID:DATASET.TABLE
—for example,myProject:myDataset.myTable
. If--destination_table
is unspecified, a query job is generated that writes the output to a temporary table.
API
To run a query using the API, insert a new job
and populate the query
job configuration property. Optionally specify your
location in the location
property in the jobReference
section of the
job resource.
When you populate the query job properties, include the
configuration.query.priority
property and set the value to BATCH
.
Poll for results by calling
getQueryResults
.
Poll until jobComplete
equals true
. Check for errors and warnings in the
errors
list.
Go
Before trying this sample, follow the Go setup instructions in the BigQuery quickstart using client libraries. For more information, see the BigQuery Go API reference documentation.
Java
To run a batch query, set the query priority to QueryJobConfiguration.Priority.BATCH when creating a QueryJobConfiguration.
Before trying this sample, follow the Java setup instructions in the BigQuery quickstart using client libraries. For more information, see the BigQuery Java API reference documentation.
Node.js
Before trying this sample, follow the Node.js setup instructions in the BigQuery quickstart using client libraries. For more information, see the BigQuery Node.js API reference documentation.
Python
Before trying this sample, follow the Python setup instructions in the BigQuery quickstart using client libraries. For more information, see the BigQuery Python API reference documentation.
For more information, see Interactive versus batch queries.
What's next
- Learn how to manage query jobs.
- Learn how to view query history.
- Learn how to save and share queries.