What This Automation Does
This workflow turns simple text requests into exact SQL queries using your MySQL database schema and AI.
It solves slow, error-prone manual querying by automating query creation just from the schema, without needing direct data access.
The result is fast, correct SQL queries and clear answers shown together.
Who Should Use This Workflow
This is for data users who are not SQL experts but need to get data from complex MySQL databases.
It fits teams with limited SQL skills or people who want to save time and avoid query mistakes.
It also works well if database is slow or schema is big because schema is saved locally for quick reuse.
Tools and Services Used
- MySQL: Stores your data and schema structure.
- OpenAI GPT-4o via LangChain: Generates SQL queries from schema and user text.
- n8n nodes: MySQL, LangChain OpenAI Chat Model, LangChain AI Agent, File Read/Write, Set, If, Merge.
Inputs, Process, and Output
Inputs
- Natural language query from a user via chat.
- Database schema extracted from MySQL.
Processing Steps
- Fetch and save schema of all tables locally to avoid repeated remote calls.
- On user query, load saved schema and combine with user text.
- Use AI agent to make SQL SELECT query from the schema only.
- Extract SQL query from AI text carefully using regex inside Set node.
- Check if valid query exists using If node.
- Run SQL query on database.
- Format SQL result as readable table string.
- Combine AI textual answer and query results for final output.
Output
- Readable SQL query results (in table format).
- AI-generated explanation or context answering the input question.
Beginner Step-by-Step: Using This Workflow in n8n Production
Step 1: Import the Workflow
- Download the workflow file using the Download button on this page.
- Open your n8n editor where you want to run the workflow.
- Click on “Import from File” and select the downloaded workflow file.
Step 2: Configure Credentials and Settings
- Add your MySQL database credentials if not already added in n8n.
- Add your OpenAI API Key with GPT-4o access under credentials.
- Update any database names, table names, or file paths in nodes if your setup differs.
Step 3: Test the Workflow
- Send a sample natural language query to the chat webhook URL created by the LangChain Chat Trigger node.
- Check the workflow run inside n8n’s execution panel for errors and results.
Step 4: Activate for Production
- Once testing is successful, toggle the Enable switch in n8n to activate the workflow.
- Use the webhook URL to receive and process live user queries.
- Optionally, schedule periodic schema extraction to keep schema data updated.
For better control, consider self-host n8n to run this on your own server.
Common Issues and Fixes
- MySQL connection errors: Check that database host, username, and password are correct.
- AI agent missing SQL query: Make sure schema JSON is passed correctly and prompt includes it fully.
- File read/write errors: Confirm file paths and that n8n has needed system permissions.
- Slow schema extraction: Be sure to save schema locally and reload saved JSON on each query.
Customization Ideas
- Change your database by updating MySQL credentials and queries in nodes.
- Adjust AI temperature in LangChain nodes to change query creativity level.
- Modify regex in Set nodes to extract other SQL commands if needed.
- Use caching nodes to store AI-generated queries for repeated user questions.
Summary of Benefits and Results
✓ Faster, error-reduced SQL query creation from natural language.
✓ Stored database schema locally improves response times and reduces remote calls.
✓ Non-technical users can write SQL queries using easy chat messages.
→ Workflow delivers both SQL results and AI answers together.

