Book a Demo Start Instantly

The integration of database technologies with advancements in artificial intelligence, particularly through Large Language Models (LLMs) such as OpenAI’s GPT-3, is a pivotal development in the realm of data management. Text-to-SQL(text2sql) capabilities enable users to convert natural language queries into precise SQL commands, democratizing data access and empowering users across varying levels of technical expertise.

Enhancing SQL Query Generation with LLMs

LLMs are at the forefront of this revolution, exhibiting an impressive ability to parse human language and generate corresponding SQL queries. This functionality is crucial for users without a background in SQL, allowing them to interact with databases more intuitively and efficiently.

Practical Examples and Table Definitions

Consider a scenario involving two essential MySQL database tables:

User Table

This table tracks users, with each record containing a unique identifier, the user’s name, and a reference to their associated school:

    UserName VARCHAR(100),
    SchoolID INT,
    FOREIGN KEY (SchoolID) REFERENCES School(SchoolID)

School Table

This table details schools, including a unique identifier for each school, its name, and location:

    SchoolName VARCHAR(255),
    Location VARCHAR(255)

Query Examples Using Text-to-SQL

With the above table definitions in mind, LLMs can assist in generating SQL queries for commonly asked questions such as:

1.Total number of schools:

SELECT COUNT(*) AS TotalSchools FROM School;

2.Number of students per school:

SELECT SchoolName, COUNT(*) AS StudentCount
FROM User JOIN School ON User.SchoolID = School.SchoolID
GROUP BY SchoolName;

These examples demonstrate the LLM’s ability to derive structured queries from natural language inputs, simplifying complex data retrieval tasks.

Crafting Effective Prompts for LLMs

To maximize the effectiveness of LLMs in SQL generation, it is essential to construct detailed prompts that include clear questions and contextual information about the database schema. Here’s a guideline on how to write these prompts:

  1. State the Question Clearly: Define precisely what information you need.
  2. Describe the Schema: Include descriptions or SQL definitions of the tables involved.
  3. Specify Any Conditions: Detail any specific conditions or filters for the query.
  4. Request SQL Generation: Clearly indicate that you need an SQL query in response.

Example Prompt:

Given the following table definitions

— 'User' with fields 'UserID' (integer, primary key), 'UserName' (varchar, up to 100 characters), and 'SchoolID' (integer, foreign key linked to 'School');
- 'School' with 'SchoolID' (integer, primary key), 'SchoolName' (varchar, up to 255 characters), and 'Location' (varchar, up to 255 characters)

could you generate an SQL query to answer question "count the number of students at each school?"


The capability of LLMs to transform natural language into executable SQL queries marks a significant advancement in the way we interact with databases. This innovation not only simplifies the querying process but also makes data more accessible to a broader audience, enhancing decision-making across various sectors. As we continue to explore the potential of these technologies, their integration promises to further streamline and enhance our data-driven world, ensuring that the complexities of database syntax are no longer a barrier to accessing valuable insights.

If you want to try this simple demo, TiDB Serverless is a good playground for you to start, because it is a MySQL-compatible database and FREE for 25GiB storage.

One more thing, TiDB Serverless has a Text-to-SQL built-in console. Read this doc to learn more.

Try Text-to-SQL with TiDB Serverless.

Start Instantly

Last updated May 30, 2024

Spin up a Serverless database with 25GiB free resources.

Start Now