Write queries with Gemini assistance
This document describes how to use AI-powered assistance in Gemini in BigQuery to help you query your data with SQL queries and Python code. Gemini in BigQuery can generate queries and code, complete queries and code while you type, and explain queries.
To follow step-by-step guidance for this task directly in the Google Cloud console, click Guide me:
Gemini for Google Cloud doesn't use your prompts or its responses as data to train its models without your express permission. For more information about how Google uses your data, see How Gemini for Google Cloud uses your data.
Only English language prompts are supported for Gemini in BigQuery.
This document is intended for data analysts, data scientists, and data developers who work with SQL queries and Colab Enterprise notebooks in BigQuery. It assumes that you know how to query data in the BigQuery Studio environment or how to work with Python notebooks to analyze BigQuery data.
Before you begin
- Ensure that Gemini in BigQuery is set up for your Google Cloud project. This step is normally done by an administrator. Gemini in BigQuery features might be turned off or unavailable until you complete the remaining steps in this section.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
In the Google Cloud console, go to the BigQuery Studio page.
In the BigQuery toolbar, click pen_spark Gemini.
In the list of features, ensure the following features are selected:
Gemini in SQL query list:
- Auto-completion (Preview). As you type in the query editor, Gemini can suggest logical next steps that are relevant to your current query's context, or it can help you iterate on a query.
- Auto-generation. You can prompt Gemini in BigQuery using a natural language comment in the BigQuery query editor to generate a SQL query.
- SQL generation tool. You can enter natural language text in a tool to generate a SQL query, with options to refine query results, choose table sources, and compare results.
- Explanation. You can prompt Gemini in BigQuery to explain a SQL query using natural language.
Gemini in Python notebook list:
- Code completion (Preview). Gemini provides contextually appropriate recommendations that are based on content in the notebook.
- Code generation. You can prompt Gemini using a natural language statement or question to generate Python code.
To complete the tasks in this document, ensure that you have the required Identity and Access Management (IAM) permissions.
Required roles
To get the permissions that you need to write queries with Gemini assistance,
ask your administrator to grant you the
Gemini for Google Cloud User (roles/cloudaicompanion.user
) IAM role on the project.
For more information about granting roles, see Manage access to projects, folders, and organizations.
This predefined role contains the permissions required to write queries with Gemini assistance. To see the exact permissions that are required, expand the Required permissions section:
Required permissions
The following permissions are required to write queries with Gemini assistance:
-
cloudaicompanion.entitlements.get
-
cloudaicompanion.instances.completeTask
-
Explain SQL queries:
cloudaicompanion.companions.generateChat
-
Complete SQL or Python code:
cloudaicompanion.instances.completeCode
-
Generate SQL or Python code:
cloudaicompanion.instances.generateCode
You might also be able to get these permissions with custom roles or other predefined roles.
For more information about IAM roles and permissions in BigQuery, see Introduction to IAM.
Generate a SQL query
To generate a SQL query based on your data's schema, you can provide Gemini in BigQuery with a natural language statement or question, also known as a prompt. Even if you're starting with no code, a limited knowledge of the data schema, or only a basic knowledge of GoogleSQL syntax, Gemini in BigQuery can generate SQL that can help you explore your data.
Use the SQL generation tool
The SQL generation tool lets you use natural language to generate a SQL query about your recently viewed or queried tables. You can also use the tool to modify an existing query, and to manually specify the tables for which you want to generate SQL.
To use the SQL generation tool, follow these steps:
In the Google Cloud console, go to the BigQuery Studio page.
Next to the query editor, click pen_spark SQL generation tool.
In the Generate SQL with Gemini dialog, enter a natural language prompt about a table that you recently viewed or queried. For example, if you recently viewed
bigquery-public-data.austin_bikeshare.bikeshare_trips
table, you might enter the following:Show me the duration and subscriber type for the ten longest trips.
Click Generate.
The generated SQL query is similar to the following:
SELECT subscriber_type, duration_sec FROM `bigquery-public-data.san_francisco_bikeshare.bikeshare_trips` ORDER BY duration_sec DESC LIMIT 10;
Review the generated SQL query and take any of the following actions:
- To accept the generated SQL query, click Insert to insert the statement into the query editor. You can then click Run to execute the suggested SQL query.
- To edit your prompt, click Edit and then modify or replace your initial prompt. After you've edited your prompt, click Update to generate a new query.
- To update the table sources that were used as context to generate the suggested SQL query, click Edit Table Sources, select the appropriate checkboxes, and then click Apply.
- To view a natural language summary of the generated query, click Query Summary.
- To refine the suggested SQL query, enter any refinements in the
Refine field, and then click
limit to 1000
to limit the number of query results. To compare the changes to your query, select the Show diff checkbox.
Refine. For example, enter
- To dismiss a suggested query, close the SQL generation tool.
Turn off the SQL generation tool
To learn how to turn off the SQL generation tool, see Turn off Gemini query assistant features.
Generate SQL from a comment
You can generate SQL in the query editor by describing the query that you want in a comment.
In the Google Cloud console, go to the BigQuery Studio page.
In the query editor, click
. SQL query In the query editor, write a SQL comment about a table you have recently viewed or queried. For example, if you recently viewed the
bigquery-public-data.austin_bikeshare.bikeshare_trips
table, then you might write the following comment:# Show me the duration and subscriber type for the ten longest trips.
Press Enter (Return on macOS).
The suggested SQL query is similar to the following:
# Show me the duration and subscriber type for the ten longest trips SELECT duration_sec, subscriber_type AVG(duration_minutes) AS average_trip_length FROM `bigquery-public-data.austin_bikeshare.bikeshare_trips` ORDER BY duration_sec LIMIT 10;
To accept the suggestion, press Tab.
Tips for SQL generation
The following tips can improve suggestions that Gemini in BigQuery provides:
- To manually specify which tables to use, you can include the fully
qualified table name in backticks (
`
), such as`PROJECT.DATASET.TABLE`
. - If the column names or their semantic relationships are unclear or complex, then you can provide context in the prompt to guide Gemini towards the answer that you want. For example, to encourage a generated query to reference a column name, describe the column name and its relevance to the answer that you want. To encourage an answer that references complex terms like lifetime value or gross margin, describe the concept and its relevance to your data to improve SQL generation results.
- When you generate SQL from a comment, you can format your prompt over
multiple lines by prefixing each line with the
#
character. - Column descriptions are considered when you generate SQL queries. To improve accuracy, add column descriptions to your schema. For more information about column descriptions, see Column descriptions in "Specify a schema."
Gemini and BigQuery data
Gemini in BigQuery can access the metadata of the tables that you have permission to access. This metadata can include the table names, column names, data types, and column descriptions. Gemini in BigQuery cannot access the data in your tables, views, or models. For more information on how Gemini uses your data, see How Gemini for Google Cloud uses your data.
Complete a SQL query
SQL completion attempts to provide contextually appropriate recommendations that are based on content in the query editor. As you type, Gemini can suggest logical next steps that are relevant to your current query's context, or it can help you iterate on a query.
To try SQL completion with Gemini in BigQuery, follow these steps:
In the Google Cloud console, go to the BigQuery Studio page.
In the query editor, copy the following:
SELECT subscriber_type , EXTRACT(HOUR FROM start_time) AS hour_of_day , AVG(duration_minutes) AS avg_trip_length FROM `bigquery-public-data.austin_bikeshare.bikeshare_trips`
An error message states that
subscriber_type
isn't grouped or aggregated. It's not uncommon to need some help getting a query just right.At the end of the line for
subscriber_type
, press Space.The suggested refinements to the query might end in text that's similar to the following:
GROUP BY subscriber_type, hour_of_day;
You can also press Enter (Return on macOS) to generate suggestions.
To accept the suggestion, press Tab, or hold the pointer over the suggested text and click through alternate suggestions. To dismiss a suggestion, press ESC or continue typing.
Explain a SQL query
You can prompt Gemini in BigQuery to explain a SQL query in natural language. This explanation can help you understand a query whose syntax, underlying schema, and business context might be difficult to assess due to the length or complexity of the query.
To get an explanation for a SQL query, follow these steps:
In the Google Cloud console, go to the BigQuery Studio page.
In the query editor, open or paste a query that you want explained.
Highlight the query that you want Gemini in BigQuery to explain.
Click astrophotography_mode Gemini, and then click Explain this query.
The SQL explanation appears in the Gemini pane.
Generate Python code
You can ask Gemini in BigQuery to generate Python code by using a prompt (a natural language statement or question). Gemini in BigQuery responds with one or more Python code suggestions.
In the following example, you generate code for a BigQuery
public dataset, bigquery-public-data.ml_datasets.penguins
.
Go to the BigQuery Studio page.
In the tab bar of the query editor, click the
drop-down arrow next to SQL query, and then click Python notebook.The new notebook opens, containing cells that show example queries against the
bigquery-public-data.ml_datasets.penguins
public dataset.To insert a new code cell, in the toolbar, click
Code. The new code cell contains the message Start coding or generate with AI.In the new code cell, click generate.
In the Generate editor, enter the following natural language prompt:
Using bigquery magics, query the `bigquery-public-data.ml_datasets.penguins` table
Press Enter (Return on macOS).
The suggested Python code is similar to the following:
%%bigquery SELECT * FROM `bigquery-public-data.ml_datasets.penguins` LIMIT 10
To run the code, press
Run cell.
Complete Python code
Python code completion attempts to provide contextually appropriate recommendations that are based on content in the query editor. As you type, Gemini in BigQuery can suggest logical next steps that are relevant to your current code's context, or it can help you iterate on your code.
To try Python code completion with Gemini in BigQuery, follow these steps:
Go to the BigQuery Studio page.
In the tab bar of the query editor, click the
drop-down arrow next to SQL query, and then click Python notebook.A new notebook opens, containing cells that show example queries against the
bigquery-public-data.ml_datasets.penguins
public dataset.In the editor, begin typing Python code. For example
%%bigquery
. Gemini in BigQuery suggests code inline while you type.To accept the suggestion, press Tab.
Turn off Gemini query assistant features
To turn off specific features in Gemini in BigQuery, do the following:
In the Google Cloud console, go to the BigQuery Studio page.
In the BigQuery toolbar, click
pen_spark Gemini .In the list, clear the query assistant features that you want to turn off.
To learn how to turn off Gemini in BigQuery, see Turn off Gemini in BigQuery.
Provide feedback
In the Google Cloud console, go to the BigQuery Studio page.
In the BigQuery toolbar, click
pen_spark Gemini .Click Send feedback.
Help improve suggestions
You can help improve Gemini suggestions by sharing with Google the prompt data that you submit to features in Preview.
To share your prompt data, follow these steps:
In the Google Cloud console, go to the BigQuery Studio page.
In the BigQuery toolbar, click
pen_spark Gemini .Select Share data to improve Gemini in BigQuery.
In the Data Use Settings dialog, update your data use settings.
Data sharing settings apply to the entire project and can only be set by a
project administrator with the serviceusage.services.enable
and
serviceusage.services.list
IAM permissions. For more
information about data use in the Trusted Tester Program, see Gemini
for Google Cloud Trusted Tester Program.
Pricing
For details about pricing for this feature, see Gemini in BigQuery pricing overview.
Quotas and limits
For information about quotas and limits for this feature, see Quotas for Gemini in BigQuery.