Write SQL with Gemini assistance

MySQL  |  PostgreSQL  |  SQL Server

Preview —Gemini Code Assist

This feature is subject to the "Pre-GA Offerings Terms" in the General Service Terms section of theService Specific Terms. You can process personal data for this feature as outlined in theCloud Data Processing Addendum, subject to the obligations and restrictions described in the agreement under which you access Google Cloud. Pre-GA features are available "as is" and might have limited support. For more information, see thelaunch stage descriptions.

This document describes how you can useGemini, an AI-powered collaborator inGoogle Cloud, to help you do the following in Cloud SQL Studio:

Learnhow and when Gemini for Google Cloud uses your data.

This document is intended for database administrators and data engineers whoare familiar with SQL, data analysis, and Cloud SQL. If you're newto Cloud SQL, seeCloud SQL overview.

Note: Coding assistance is part ofGemini Code Assist and is available atno 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 acquireaGemini Code Assist Standard edition license to continue touse coding assistance in Cloud SQL Studio.

Before you begin

  1. To complete the tasks in this document, ensure that you have thenecessary Identity and Access Management (IAM) permissions.
  2. Confirm access to Cloud SQL Studio. If you don't have access toCloud SQL Studio, thenseeCloud SQL Studio required roles and permissions
  3. Optional: To follow along with the examples in this document,create aSingers table using the following schema:

    CREATETABLESingers(SingerIdBIGINTPRIMARYKEY,FirstNameVARCHAR(1024),LastNameVARCHAR(1024),SingerInfoBYTEA,BirthDateTIMESTAMPTZ);

    After you create theSingers table, clickrefreshRefresh toupdate your database schema.

Required roles and permissions

To get the permissions that you need to complete the tasks in this document, ask your administrator to grant you theCloud SQL Admin (roles/cloudsql.admin) IAM role on the project. For more information about granting roles, seeManage access to projects, folders, and organizations.

You might also be able to get the required permissions throughcustom roles or otherpredefined roles.

You also need thecloudaicompanion.companions.generateChat permission to useexplain SQL statements in the query editor andcloudaicompanion.companions.generateCode to usehelp me code.

Generate SQL queries using natural language prompts

As an early-stage technology, Gemini for Google Cloud products can generate output that seems plausible but is factually incorrect. We recommend that you validate all output from Gemini for Google Cloud products before you use it. For more information, seeGemini for Google Cloud and responsible AI.

You can give Gemini natural language comments (orprompts) togenerate queries that are based on your schema. For example, you can promptGemini 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?"

If your schema changes, such as addition of a new table or column, thenclickrefreshRefresh to updateyour schema before using Gemini.

Note: When you enter a prompt for Gemini inCloud SQL, information about your database's schema is includedwith the prompt. This metadata can include table and column names, data types,and column descriptions. Your database data remains in Cloud SQLand isn't sent to Gemini.

To generate SQL in Cloud SQL with Geminiassistance, follow these steps:

  1. In the Google Cloud console, go to theCloud SQL page.

    Go to Cloud SQL

  2. Select an instance from the list.

  3. In the navigation menu, clickCloud SQL Studio.

  4. Sign in to Cloud SQL Studio using the name of your database, username, and password.TheExplorer pane displays a list of objects in your database.

  5. In the taskbar, clickpen_sparkGemini to viewGemini features in Cloud SQL.

  6. Ensure thatComment-to-query generation is enabled.

  7. To query your database, clickNew SQL editor tab.

  8. To generate SQL, type a comment in the query editor startingwith-- followed by asingle-line comment,and then pressReturn orEnter.

    For example, enter the following prompt:

    -- add a row to table singers

    PressReturn orEnter. Gemini generates SQL that'ssimilar to the following:

    INSERTINTOSingers(SingerId,FirstName,LastName,BirthDate)VALUES(1,'Alex','M.','1977-10-16');

    To continue the example using theSingers table, enter the followingprompt:

    -- show all singers born in the 70s

    Geminigenerates SQL that's similar to the following:

    SELECT*FROMSingersWHERESingers.BirthDateBETWEEN'1970-01-01'AND'1979-12-31'
    Note: Gemini might suggest different queries each time thatyou enter the same prompt.
  9. Review the SQL suggestion and take any of the following actions:

    Caution: Because Data Manipulation Language (DML) and Data DefinitionLanguage (DDL) queries generated by Gemini can overwritedata, we recommend that you validate the generated queries before yourun the queries.
    • To view the options for accepting the SQL generated by Gemini,hold the pointer over the query. The following options are displayed:
      • Accept: To accept the suggested query, pressTab,then clickRun.
      • Accept word: To accept the suggested query partially,pressControl+Right arrow.(orCommand+Right arrow on macOS), then clickRun.
    • To edit the original SQL, pressTab, edit the SQL, andthen clickRun.
    • To dismiss the suggestion, pressEsc or continue typing.

Help me code tool

To use theHelp me code tool, follow these steps:

  1. In the Google Cloud console, go to theCloud SQL page.

    Go to Cloud SQL

  2. Select an instance from the list.

  3. In the navigation menu, clickCloud SQL Studio.

  4. Sign in to Cloud SQL Studio using the name of your database, username, and password.TheExplorer pane displays a list of objects in your database.

  5. To query your database, click theNew tab.

  6. Clickpen_sparkHelp me code next to the query editor.

  7. In theHelp me code window, enter a prompt. For example,add a row totable singers and clickGenerate.

    Gemini generates SQL that's similar to the following:

    INSERTINTOSingers(SingerId,FirstName,LastName,BirthDate)VALUES(1,Alex,'M.','1977-10-16');
    Note: Gemini might suggest different syntax each time thatyou enter the same prompt.
  8. Review the generated SQL and take any of the following actions:

    Caution: Because Data Manipulation Language (DML) and Data DefinitionLanguage (DDL) queries generated by Gemini can overwritedata, we recommend that you validate the generated queries before yourun the queries.
    • To accept SQL generated by Gemini, clickInsert toinsert the statement into the query editor. Then clickRun toexecute the suggested SQL.
    • To ask Gemini to generate a new query, clickEdit.After you've edited your prompt, clickUpdate. You can then decideto accept the new generated statement or dismiss the suggestion.
    • To dismiss the suggestion, close theHelp me code window.

Explain SQL statements in the query editor

You can use Gemini in Cloud SQL to explain SQL queriesin natural language. This explanation can help you understand the syntax,underlying schema, and business context for complex or long queries.

  1. In the Google Cloud console, go to theCloud SQL page.

    Go to Cloud SQL

  2. Select an instance from the list.

  3. In the navigation menu, clickCloud SQL Studio.

  4. Sign in to Cloud SQL Studio using the name of your database, username, and password.TheExplorer pane displays a list of objects in your database.

  5. To query your database, click theNew tab.

  6. In the query editor, paste the query you want explained.

  7. Highlight the query that you want Gemini to explain, andthen clicksparkExplain this query.

    The explanation for the SQL query appears in the GeminiChat window.

What's next

Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2025-07-14 UTC.