Natural language to SQL in the Azure portal query editor (preview)

Applies to: Azure SQL Database

Part of the Copilot in Azure SQL Database (preview), the Natural language to SQL experience within the Azure portal query editor translates natural language queries into SQL, making database interactions more intuitive.

In this article, learn how to write natural language prompts to generate T-SQL queries using Copilot for Azure SQL Database in the Azure portal query editor.

Warning

Copilot in Azure SQL Database is a preview set of experiences that are powered by large language models (LLMs). Output produced by Copilot might contain inaccuracies, biases, or other unintended content. This occurs because the model powering Copilot was trained on information from the internet and other sources. As with any generative AI model, humans should review the output produced by Copilot before use.

Natural language to SQL query

Copilot in Azure SQL Database utilizes table and view names, column names, primary key, and foreign key metadata to generate T-SQL code. You then review and execute the code suggestion.

Note

Copilot in Azure SQL Database does not use data values to generate Transact-SQL suggestions.

This integration means that Copilot can answer questions with prompts like:

  • Which agents have listed more than two properties for sale?
  • Tell me the rank of each agent by property sales and show name, total sales, and rank
  • Even advanced scenarios such as Show me a pivot summary table that displays the number of properties sold in each year from 2020 to 2023

Prerequisites

Important

Review these preview terms before using natural language to SQL in Copilot in Azure SQL Database.

Generate a query

As a preview feature, once enrolled, you can find Copilot in Azure SQL Database integrated with the query editor.

To use natural language to SQL in Copilot in Azure SQL Database, follow these steps:

  1. In the query editor toolbar, select the Launch inline copilot button.
  2. In the Table selection dropdown menu, select the table and views for consideration by Copilot.
  3. Type your question in the input box. When ready, select the Generate Query button. This will generate the suggested T-SQL code statement.
  4. Select Accept or Decline for the generated T-SQL query.
  5. Execute the generated T-SQL query by selecting Run, or you can edit the prompt and regenerate new code. This regeneration will append, and not replace, any existing code in your query editor window.

Note

This feature currently only supports the generation of SELECT statements.

Give feedback

We use feedback on generated queries to help improve Copilot in Azure SQL Database. This feedback is crucial to improving the quality of the suggestions.

  1. To send feedback on queries, select the thumbs-up or thumbs-down beneath the Generate Query button.
  2. Within the feedback dialog, provide the nature of the feedback, a description of what went right or wrong, and then an option to share your prompts with Microsoft.
  3. Select Submit.

Write effective prompts

Here are some tips for writing effective prompts.

  • When crafting prompts, be sure to start with a clear and concise description of the specific information you're looking for.

  • Natural language to SQL depends on expressive table and column names. If your table and columns aren't expressive and descriptive, Copilot might not be able to construct a meaningful query.

  • Use natural language that is applicable to your table and view names, column names, primary keys, and foreign keys of your database. This context helps Copilot generate accurate queries. Specify what columns you wish to see, aggregations, and any filtering criteria as explicitly as possible. Copilot should be able to correct typos or understand context given your schema context.

  • To avoid ambiguity or incorrect table selections, consider filtering down the specific tables of interest in the table selection dropdown list.

  • Avoid ambiguous or overly complex language in your prompts. Simplify the question while maintaining its clarity. This editing ensures Copilot can effectively translate it into a meaningful T-SQL query that retrieves the desired data from the associated tables and views.

  • Currently, natural language to SQL supports English language to T-SQL.

  • The following example prompts are clear, specific, and tailored to the properties of your schema and database, making it easier for Copilot to generate accurate Transact-SQL queries:

    • Show me all properties that sold last year
    • Count all the products, group by each category
    • Show all agents who sell properties in California
    • Show agents who have listed more than two properties for sale
    • Show the rank of each agent by property sales and show name, total sales, and rank
    • Show me a pivot summary table that displays the number of properties sold in each year from 2020 to 2023