Write and edit a query

If you want to perform more complex analyses (e.g. join data from more than one BigQuery table), you can write a custom query.

Important:

  1. On your computer, open a spreadsheet in Google Sheets.
  2. In the menu at the top, click Data and then Data connectors and then Connect to BigQuery.
  3. Select a project.
  4. Click Saved queries and query editor.
  5. Enter a query.
    • To preview how much data your query scans, in the bottom right of the editor, click Preview results.
  6. Click Insert results.

Tips:

Use saved queries in connected sheets

To create queries in connected sheets, you can access saved queries from BigQuery projects. Learn more about saved queries.

  1. On your computer, open a spreadsheet in Google Sheets.
  2. In the menu, at the top of the spreadsheet, click Data and then Data connectors and then Connect to BigQuery.
  3. Select a project.
  4. Click Saved queries and query editor and then BigQuery saved queries.
  5. Select a project and then select a saved query.
  6. Click Connect.

To edit a saved query, update it directly within BigQuery. The updated query will appear in connected sheets within five minutes.

To keep your saved queries up to date whenever you update saved queries in BigQuery:

  1. In the connected sheet, sync your saved queries through Sync from BigQuery.
  2. In the 'BigQuery editor', click Connect.

Learn more about saved queries

When an error occurs due to a modified or deleted saved query, you can update the connection from the error dialogue or BigQuery editor.

  1. At the top right of the preview sheet, open Connection settings.
  2. To update the connection, click Sync from BigQuery and then Connect.
  3. To update the connection for the following cases:
    • Modified query: Click View error and then Sync saved query.
      • Or you can open the query editor. Click Edit and then Sync saved query and then Connect.
    • Deleted query: Click View error and then Use as custom query.
      • Or you can:
        1. To open the query editor, click Edit.
        2. Change to another query or unlink the existing query.
        3. Click Connect.
Add parameters to your query

To use the value of a cell in a query, set up a parameter.

  1. At the right of the query editor, click Parameters and then Add.
  2. Enter a parameter name and the cell you want to reference.
  3. Click Add.
When you compare data in Sheets and BigQuery
To compare data, you might need to change the data type. For example, if a column is string data in BigQuery, then it must be plain text in Sheets to be compared correctly.
If the data type in the spreadsheet and BigQuery are different, you can do the following:

Sample queries

Simple query:

SELECT
word,
SUM(word_count) AS word_count
FROM
`bigquery-public-data.samples.shakespeare`
WHERE word IN ('me', 'I', 'you')
GROUP BY word;

Parameterised query:

SELECT SUM(kw_total)
FROM `bigquery-public-data.sunroof_solar.solar_potential_by_censustract`
WHERE state_name = @STATENAME;

Next: Fix problems with BigQuery data in Sheets

Related resources

true
Visit the Learning Center

Using Google products, like Google Docs, at work or school? Try powerful tips, tutorials, and templates. Learn to work on Office files without installing Office, create dynamic project plans and team calendars, auto-organize your inbox, and more.

Search
Clear search
Close search
Google apps
Main menu
10687133597181810348
true
Search Help Centre
true
true
true
true
true
35
false
false
false