This article introduces how to use the Text to SQL feature in the MaiAgent system to help you easily query database information using everyday conversation
What is Text to SQL?
Text to SQL (also known as Text2SQL) is an intelligent tool that automatically converts natural language questions (everyday human language) into SQL database query statements. Simply put, it enables AI assistants to "understand human language" and directly operate databases.
You: "Help me check how many bottles of cola were sold yesterday"
Employee: "Boss, you need to teach me how to use the POS system to query..."
You: "Click here, select there, enter conditions..."
Requires hands-on teaching, which is time-consuming.
With Text2SQL:
You: "Help me check how many bottles of cola were sold yesterday"
AI Employee: "Sure!" 💫 (automatically generates query method and calls database) → "87 bottles of cola were sold yesterday" ✅
Core Functions of Text to SQL
Natural language question → AI understanding and analysis → SQL query statement → Execute query → Return results
When you ask the AI assistant "Find yesterday's best-selling beverage," the AI assistant first analyzes your question, understands you're looking for a "beverage" that was the "best-selling" "yesterday," then generates SQL syntax to query the database, and finally tells you the result: "Coca-Cola."
What Text to SQL Can Help AI Accomplish
Specific Application Scenarios
🏥 Medical Clinic
🏫 School Management
🏪 Retail Chain Store
Advantages of Text to SQL
⚡ Efficiency Improvement
Traditional approach: Requires SQL expert to write queries → 30 minutes
Text2SQL: Natural language questions → 3 seconds
🎯 Lower Barrier to Entry
No need to learn complex SQL syntax
No need to memorize database structure
Anyone can query data
📱 Real-time Interaction
Get answers immediately after asking
Support for follow-up questions
Dynamic adjustment of query conditions
How to Use Text to SQL Feature in MaiAgent
Before using, you need to prepare: Enterprise-associated database or Excel file (upload to knowledge base)
If using a database, we recommend:
Ensure clear data table structure
Create appropriate indexes
Maintain data quality
1. Enter AI Assistant Settings
Select the AI assistant you want to configure
Switch to the response mode settings page
2. Switch AI Assistant's Response Mode to Agent Mode
You must switch to Agent mode, otherwise the AI assistant cannot use the Text to SQL feature
For detailed introduction of each response mode, please refer to: Create AI Assistant
3. Enter Enterprise Database URL
Use the dropdown menu to select the database service used within the enterprise
Enter the database service URL used by the enterprise to allow the MaiAgent system to connect and operate within the database
MaiAgent supports:
MySQL
PostgreSQL
Oracle DB
Microsoft SQL Server (MSSQL)
The maiagent option applies Excel files you have uploaded in the MaiAgent knowledge base
Please ensure the URL format is correct and includes necessary connection information, such as: host name, port, database name, username, and password.
Connect to an existing MSSQL database by pasting the MSSQL database connection string
📍Please note: Ensure the database URL is accessible by MaiAgent service
MySQL
Connect to an existing MySQL database by pasting the MySQL database connection string
📍Please note: Ensure the database URL is accessible by MaiAgent service
Oracle
Connect to an existing Oracle database by pasting the Oracle database connection string
📍Please note: Ensure the database URL is accessible by MaiAgent service
PostgreSQL
Connect to an existing PostgreSQL database by pasting the PostgreSQL database connection string
📍Please note: Ensure the database URL is accessible by MaiAgent service
4. Click Save to Preserve 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: Confirm table names and column names are correct
Insufficient permissions: Check database user permission settings
Text2SQL turns AI assistants into database experts, enabling anyone to use natural language to quickly gain business insights, dramatically improving the efficiency of data-driven decision-making!
Doctor asks: "Find today's diabetes patient appointments"
AI responds: "There are 5 diabetes patients scheduled today:
- Mr. Wang 10:00 Follow-up visit
- Mrs. Lee 14:30 Follow-up examination
- Miss Chen 16:00 Blood glucose monitoring"
Teacher asks: "How many students failed this exam?"
AI responds: "This math exam:
- Total students: 45
- Failed: 8 (17.8%)
- Student list for makeup exam has been compiled"
Store manager asks: "Compare monthly revenue of three branches"
AI responds: "November branch revenue comparison:
- Taipei Store: $1,200,000 (↑15%)
- Taichung Store: $950,000 (↑8%)
- Kaohsiung Store: $800,000 (↓3%)"