Text to SQL Feature

This article introduces how to use Text to SQL functionality in the MaiAgent system to help you easily query database information through daily conversations

What is Text to SQL?

Text to SQL (also known as Text2SQL) is a smart tool that automatically converts natural language questions (human everyday language) into SQL database query statements. Simply put, it enables AI assistants to "understand human language" and directly operate databases.

To learn more about Text2SQL, please see: Technical Manual—Text to SQL

Imagine you're a convenience store owner:

Traditional Method:

  • You: "Help me check how many bottles of Coke were sold yesterday"

  • Employee: "Boss, you need to teach me how to query the POS system..."

  • You: "Click here, select there, enter conditions..."

Requires hands-on teaching, more time-consuming.

With Text2SQL:

  • You: "Help me check how many bottles of Coke were sold yesterday"

  • AI Employee: "Sure!" 💫 (Automatically generates query method and calls database) → "87 bottles of Coke were sold yesterday" ✅

Core Functions of Text to SQL

Natural Language Question → AI Understanding & Analysis → SQL Query Statement → Execute Query → Return Results

When you ask the AI assistant "Find the best-selling beverage yesterday," the AI assistant first analyzes your question, understands you're looking for "beverages" that sold "best" "yesterday," then generates SQL syntax to query the database, and finally tells you the result "Coca-Cola."

What AI Can Do with Text to SQL

Specific Application Scenarios

🏥 Medical Clinics

Doctor asks: "Find today's diabetic patients with appointments"
AI responds: "There are 5 diabetic patients with appointments today:
- Mr. Wang 10:00 Follow-up
- Mrs. Lee 14:30 Check-up
- Ms. Chen 16:00 Blood Sugar Monitoring"

🏫 School Management

Teacher asks: "How many students failed this exam?"
AI responds: "For this math exam:
- Total students: 45
- Failed: 8 (17.8%)
- List of students requiring makeup exams has been prepared"

🏪 Retail Chain Stores

Store manager asks: "Compare the monthly revenue of three branches"
AI responds: "November revenue comparison:
- Taipei Store: $1,200,000 (↑15%)
- Taichung Store: $950,000 (↑8%)
- Kaohsiung Store: $800,000 (↓3%)"

Advantages of Text to SQL

⚡ Efficiency Improvement

  • Traditional Method: Requires SQL expert to write queries → 30 minutes

  • Text2SQL: Natural language inquiry → 3 seconds

🎯 Lower Barrier

  • No need to learn complex SQL syntax

  • No need to memorize database structure

  • Anyone can query data

📱 Real-time Interaction

  • Immediate answers to questions

  • Supports follow-up questions

  • Dynamic query condition adjustment

How to Use Text to SQL in MaiAgent

Before use, you need to prepare: Enterprise relational database or Excel file (uploaded to knowledge base)

If using a database, it's recommended to:

  • Ensure clear table structure

  • Create appropriate indexes

  • Maintain data quality

1. Enter AI Assistant Settings

  • Select the AI assistant to configure

  • Switch to the answer mode settings page

2. Switch AI Assistant's Answer Mode to Agent Mode

3. Enter Enterprise Database URL

  • Use the dropdown menu to select the database service used in your enterprise

  • Enter the database service URL used by your enterprise to allow the MaiAgent system to connect to and operate within the database

  • MaiAgent supports:

    • MySQL

    • PostgreSQL

    • Oracle DB

    • Microsoft SQL Server (MSSQL)

  • maiagent option applies Excel files you've uploaded to the MaiAgent knowledge base

4. Click Save to Store Settings

This way, the AI assistant can help you quickly query your inventory, employee information, etc., and compile well-organized reports and trends for you.

Common Troubleshooting

  • Connection Failure: Check database URL format and network connectivity

  • Query Error: Verify table and column names are correct

  • Insufficient Permissions: Check database user permission settings

  • Slow Response: Check query complexity, consider adding indexes


Text2SQL transforms AI assistants into database experts, allowing anyone to quickly gain business insights using natural language, greatly improving the efficiency of data-driven decision making!

Last updated

Was this helpful?