> For the complete documentation index, see [llms.txt](https://docs.maiagent.ai/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.maiagent.ai/tech/en/advanced-genai-tech/text-to-sql.md).

# Text to SQL

## What is Text to SQL?

Text2SQL is an innovative technology powered by generative AI that aims to transform how technical professionals interact with databases. Traditionally, extracting specific information from a database required SQL expertise and manually written queries. Text2SQL allows users to enter everyday natural-language questions, which an AI engine automatically converts into precise SQL queries and uses to retrieve results from your database. This significantly lowers the barrier to data access, enabling technical professionals without an SQL background to easily obtain the data they need and work more efficiently.

<figure><img src="https://3415477754-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FNBTi475lqozGpB7xObpE%2Fuploads%2Fgit-blob-f46f2be96823acc4e50dd7fb47ed120788db81c8%2Fimage%20(30).png?alt=media" alt=""><figcaption></figcaption></figure>

## The Core Value and Importance of Text2SQL

In a data-driven era, obtaining data insights quickly and conveniently is essential. Text2SQL provides the following core benefits:

* **Lower barriers:** Reduces the technical barriers to data queries, allowing business users, analysts, and even managers without SQL skills to interact directly with data and obtain real-time information.
* **Improve efficiency:** For technical professionals familiar with SQL, Text2SQL saves considerable time on routine or repetitive queries, allowing them to focus on more complex data analysis and system architecture work.
* **Accelerate decision-making:** Real-time data querying helps businesses respond to market changes more quickly and make better business decisions.
* **Reduce errors:** Automatically generated SQL statements can help reduce syntax errors that may occur when queries are written manually.

## How Text2SQL Works

Although implementation details may vary by model, Text2SQL generally involves the following key steps:

1. Natural Language Understanding (NLU): The AI model first parses the user's natural-language question and identifies its keywords, entities, intent, and the relationships among them.
2. Schema Linking: The model must understand the database structure, including its tables, the columns in each table, their data types, and any potential relationships. This step maps terms in the question to specific tables and columns.
3. SQL Statement Generation: Based on its understanding of the question's intent and the database structure, the model constructs a query that complies with SQL syntax. This may involve selecting appropriate clauses such as SELECT, FROM, WHERE, GROUP BY, and ORDER BY.
4. Query Execution and Result Presentation (Optional): The generated SQL statement can be sent directly to the database for execution, and the query results can then be returned to the user.

## Unique Advantages of MaiAgent's Text2SQL

Now that you understand the fundamentals and applications of Text2SQL, let's look at how MaiAgent provides powerful, easy-to-use Text2SQL capabilities. MaiAgent is designed to simplify data access and offers the following key advantages:

### **Broad Database Compatibility**

MaiAgent understands the diversity of enterprise data environments. Its Text2SQL feature **currently supports several mainstream relational database systems**, including:

* **MySQL**
* **PostgreSQL**
* **Oracle DB**
* **Microsoft SQL Server (MSSQL)**

Regardless of which common database stores your data, MaiAgent can connect to it seamlessly and provide a consistent natural-language query experience.

### **Seamless Spreadsheet Data Integration**

In addition to traditional databases, MaiAgent recognizes that many temporary or small datasets are stored in spreadsheets. MaiAgent therefore provides an innovative capability: **it can automatically convert spreadsheet data (supporting .xlsx, .xls, and .csv formats) into a temporary, queryable database.**

Users only need to upload a spreadsheet file. MaiAgent parses its structure and lets you ask questions about it in natural language, just as you would query a standard database. This greatly expands the applications of Text2SQL, allowing unstructured or semi-structured data to be used quickly.

### **Minimal Setup for a Quick Start**

MaiAgent is designed to make configuration as simple as possible. To connect your database to Text2SQL, you **only need to provide the database connection URL** (Connection String/URL). There is no need for complex driver installation, environment variable setup, or other cumbersome configuration.

This plug-and-play approach lets technical professionals quickly deploy MaiAgent in an existing environment and allows end users to immediately start querying in natural language.

<figure><img src="https://3415477754-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FNBTi475lqozGpB7xObpE%2Fuploads%2Fgit-blob-e8e182af20f70727a4f973a42e7ad94586e07cdb%2F%E6%88%AA%E5%9C%96%202025-05-09%20%E4%B8%AD%E5%8D%8812.21.49.png?alt=media" alt=""><figcaption></figcaption></figure>

### Intelligent Schema Understanding

MaiAgent's core AI engine offers powerful schema-understanding capabilities. After connecting to your database, it can **automatically detect and understand every table in the database, including column structures, data types, and potential relationships.**

This automated understanding eliminates the effort of manually labeling or configuring the schema and ensures that Text2SQL can accurately map natural-language questions to the correct data entities.

### Flexible Query Scope

Although MaiAgent can automatically understand the structure of an entire database, users may sometimes be interested in only a few specific tables. MaiAgent accounts for this by allowing users to **specify which tables to search.**

Defining the query scope not only improves query relevance and accuracy by avoiding searches in unrelated tables, but also increases query efficiency in large databases and returns results faster.

## Conclusion

MaiAgent's Text2SQL feature empowers your team to work with data effortlessly. With broad database support, unique spreadsheet integration, a streamlined configuration process, intelligent schema understanding, and flexible query scope, MaiAgent is a valuable assistant throughout your data exploration and analysis journey. We encourage technical professionals to take full advantage of these features and unlock the full potential of their organization's data.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.maiagent.ai/tech/en/advanced-genai-tech/text-to-sql.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
