Run a query
This document shows you how to run a query in BigQuery and understand how much data the query will process before execution by performing a dry run.
Types of queries
You can query BigQuery data by using one of the following query job types:
Interactive query jobs. By default, BigQuery runs queries as interactive query jobs, which are intended to start executing as quickly as possible.
Batch query jobs. Batch queries have lower priority than interactive queries. When a project or reservation is using all of its available compute resources, batch queries are more likely to be queued and remain in the queue. After a batch query starts running, the batch query runs the same as an interactive query. For more information, see query queues.
Continuous query jobs. With these jobs, the query runs continuously, letting you analyze incoming data in BigQuery in real time and then write the results to a BigQuery table, or export the results to Bigtable or Pub/Sub. You can use this capability to perform time sensitive tasks, such as creating and immediately acting on insights, applying real time machine learning (ML) inference, and building event-driven data pipelines.
You can run query jobs by using the following methods:
- Compose and run a query in the Google Cloud console.
- Run the
bq querycommand in the bq command-line tool. - Programmatically call the
jobs.queryorjobs.insertmethod in the BigQuery REST API. - Use the BigQuery client libraries.
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 to projects, folders, and organizations.
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.createon the project from which the query is being run, regardless of where the data is stored. -
bigquery.tables.getDataon 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.
Troubleshooting
Access Denied: Project [project_id]: User does not have bigquery.jobs.create
permission in project [project_id].
This error occurs when a principal lacks permission to create a query jobs in the project.
Resolution: An administrator must grant you the bigquery.jobs.create
permission on the project you are querying. This permission is required in
addition to any permission required to access the queried data.
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 SQL query.
In the query editor, enter a valid GoogleSQL query.
For example, query the BigQuery public dataset
usa_namesto determine the most common names in the United States between the years 1910 and 2013:SELECT