In the taskbar, click pen_sparkGemini to view Gemini features in
Spanner.
Select the Gemini features that you want to enable—for
example,
Comment-to-query generation.
You can select and try features for yourself without affecting others
working in your project.
Optional: If you want to follow along with the examples in this document,
first create the Singers table as described in
Create a schema for your database.
To disable Gemini features in Spanner, repeat
these steps, and then deselect the Gemini features that you want
to disable.
Generate SQL queries using natural language prompts
You can give Gemini natural language comments (or prompts) to
generate queries that are based on your schema. For example, you can prompt
Gemini to generate SQL in response to the following prompts:
"Create a table that tracks customer satisfaction survey results."
"Add a date column called birthday to the Singers table."
"How many singers were born in the 90s?"
To generate SQL in Spanner with Gemini
assistance, follow these steps:
In the Google Cloud console, go to the Spanner page.
To continue the example using the Singers table, if you enter the
prompt -- show all singers born in the 70s, then Gemini
generates SQL that's similar to the following:
Review the generated SQL and take any of the following actions:
To accept SQL generated by Gemini, press
Tab, and then click Run to execute the suggested SQL.
To edit the SQL generated by Gemini, press
Tab, edit the SQL, and then click Run.
To dismiss the suggestion, press Esc or continue typing.
Explain SQL statements in the query editor
You can use Gemini in Spanner to explain SQL queries
in natural language. This explanation can help you understand the syntax,
underlying schema, and business context for complex or long queries.
In the Google Cloud console, go to the Spanner page.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-08-23 UTC."],[],[],null,["# Write SQL with Gemini assistance\n\n| **Preview\n| --- [Gemini](/gemini/docs/overview) in Spanner**\n|\n|\n| This feature is subject to the \"Pre-GA Offerings Terms\" in the General Service Terms section\n| of the [Service Specific Terms](/terms/service-terms#1).\n|\n| Pre-GA features are available \"as is\" and might have limited support.\n|\n| For more information, see the\n| [launch stage descriptions](/products#product-launch-stages).\n\nThis document describes how you can use\n[Gemini Code Assist](/gemini/docs/databases/overview) to get AI-powered\nassistance with the following in Spanner:\n\n- [Generate SQL queries using natural language prompts.](#generate-sql-queries-using-natural-language-prompts)\n- [Explain SQL queries in the query editor.](#explain-sql-in-query-editor)\n\nLearn [how and when Gemini\nfor Google Cloud uses your data](/gemini/docs/discover/data-governance).\n\nThis document is intended for database administrators and data engineers who\nare familiar with Spanner, SQL, and data analysis. If you're new\nto Spanner, see\n[Create and query a database by using the Google Cloud console](/spanner/docs/create-query-database-console).\n| **Note** : Coding assistance is part of Gemini Code Assist and is available at no charge until it's included in Gemini Code Assist Standard edition. This change will be communicated at a later date. At that time, you will need to acquire a [Gemini Code Assist Standard edition license](/gemini/docs/discover/set-up-gemini#purchase-subscription) to continue to use coding assistance in Spanner Studio.\n\nBefore you begin\n----------------\n\n1. Optional: [Set up Gemini Code Assist](/gemini/docs/discover/set-up-gemini).\n\n2. To complete the tasks in this document, ensure that you have the\n [necessary Identity and Access Management (IAM) permissions](#required-roles).\n\n3. In the Google Cloud console, go to the **Spanner** page.\n\n [Go to Spanner](https://console.cloud.google.com/spanner)\n4. Select an instance from the list.\n\n5. Select a database.\n\n6. In the navigation menu, click **Spanner Studio**.\n\n7.\n In the taskbar, click pen_spark\n **Gemini** to view Gemini features in\n Spanner.\n\n8. Select the Gemini features that you want to enable---for\n example,\n [**Comment-to-query generation**](#generate-sql-queries-using-natural-language-prompts).\n You can select and try features for yourself without affecting others\n working in your project.\n\n9. Optional: If you want to follow along with the examples in this document,\n first create the `Singers` table as described in\n [Create a schema for your database](/spanner/docs/create-query-database-console#create-schema).\n\nTo disable Gemini features in Spanner, repeat\nthese steps, and then deselect the Gemini features that you want\nto disable.\n\n### Required roles\n\n\nTo get the permissions that\nyou need to complete the tasks in this document,\n\nask your administrator to grant you the\n\n\n[Gemini for Google Cloud User](/iam/docs/roles-permissions/cloudaicompanion#cloudaicompanion.user) (`roles/cloudaicompanion.user`)\nIAM role on the project.\n\n\nFor more information about granting roles, see [Manage access to projects, folders, and organizations](/iam/docs/granting-changing-revoking-access).\n\n\nYou might also be able to get\nthe required permissions through [custom\nroles](/iam/docs/creating-custom-roles) or other [predefined\nroles](/iam/docs/roles-overview#predefined).\n\nGenerate SQL queries using natural language prompts\n---------------------------------------------------\n\n| As an early-stage technology, Gemini for Google Cloud\n| products can generate output that seems plausible but is factually incorrect. We recommend that you\n| validate all output from Gemini for Google Cloud products before you use it.\n| For more information, see\n| [Gemini for Google Cloud and responsible AI](/gemini/docs/discover/responsible-ai).\n\nYou can give Gemini natural language comments (or *prompts*) to\ngenerate queries that are based on your schema. For example, you can prompt\nGemini to generate SQL in response to the following prompts:\n\n- \"Create a table that tracks customer satisfaction survey results.\"\n- \"Add a date column called birthday to the Singers table.\"\n- \"How many singers were born in the 90s?\"\n\n| **Note:** When you enter a prompt for Gemini in Spanner, information about your database's schema is included with the prompt. This can include table and column names, data types, and column descriptions. Your database schema and data remain in Spanner and aren't sent to Gemini.\n\nTo generate SQL in Spanner with Gemini\nassistance, follow these steps:\n\n1. In the Google Cloud console, go to the **Spanner** page.\n\n [Go to Spanner](https://console.cloud.google.com/spanner)\n2. Select an instance from the list.\n\n3. Select a database.\n\n4. In the navigation pane, click **Spanner Studio** . The **Explorer**\n pane displays a list of objects in your database.\n\n5. To query your database, click the add\n **New SQL editor tab** . Make sure that\n [SQL generation is enabled](#before-you-begin).\n\n6. To generate SQL, type a comment in the query editor starting\n with `--` followed by a\n [single-line comment](/bigquery/docs/reference/standard-sql/lexical#comments),\n and then press `Return`.\n\n For example, if you enter the prompt `-- add a row to table singers` and\n press `Return`, then Gemini generates SQL that's\n similar to the following: \n\n INSERT INTO Singers (SingerId, FirstName, LastName, BirthDate)\n VALUES (1, Alex, 'M.', '1977-10-16');\n\n To continue the example using the `Singers` table, if you enter the\n prompt `-- show all singers born in the 70s`, then Gemini\n generates SQL that's similar to the following: \n\n SELECT *\n FROM Singers\n WHERE Singers.BirthDate\n BETWEEN '1970-01-01' AND '1979-12-31'\n\n | **Note:** Gemini might suggest different syntax each time that you enter the same prompt.\n7. Review the generated SQL and take any of the following actions:\n\n - To accept SQL generated by Gemini, press `Tab`, and then click **Run** to execute the suggested SQL.\n - To edit the SQL generated by Gemini, press `Tab`, edit the SQL, and then click **Run**.\n - To dismiss the suggestion, press `Esc` or continue typing.\n\nExplain SQL statements in the query editor\n------------------------------------------\n\nYou can use Gemini in Spanner to explain SQL queries\nin natural language. This explanation can help you understand the syntax,\nunderlying schema, and business context for complex or long queries.\n\n1. In the Google Cloud console, go to the **Spanner** page.\n\n [Go to Spanner](https://console.cloud.google.com/spanner)\n2. Select an instance from the list.\n\n3. Select a database.\n\n4. In the navigation pane, click **Spanner Studio**.\n\n5. To query your database, click the add\n **New tab**.\n\n6. In the query editor, paste the query.\n\n7. Highlight the query that you want Gemini to explain, and\n then click spark\n **Explain this query**.\n\n The SQL explanation appears in the **Gemini** pane.\n\nWhat's next\n-----------\n\n- Read [Gemini for Google Cloud overview](/gemini/docs/overview).\n- Learn [how Gemini uses your\n data](/gemini/docs/discover/data-governance)."]]