Natural Language MySQL Database Query Agent using GPT - n8n Workflow

Use this powerful n8n workflow template to query your MySQL database using plain English via an AI Agent powered by OpenAI. This n8n workflow integrates multiple n8n node types for advanced data access.

Workflow Preview

Ready to automate?

Download this n8n workflow template and start using it instantly.

Who is this best for?

Data Analysts who need a rapid, conversational way to extract information without writing complex SQL.
Technical Leaders looking to create self-service data access tools for non-technical teams.
n8n Automation Specialists developing powerful AI-driven n8n templates.
Developers seeking to implement advanced RAG and Tool-use patterns within an n8n workflow.

Overview

Data access often requires specialized knowledge, typically SQL proficiency. This sophisticated n8n workflow template bridges that gap, allowing any user to query complex MySQL databases using simple, plain English questions. By leveraging an advanced Langchain-based AI Agent, this n8n automation first intelligently checks the database schema using a specific n8n node (the 'SQL DB - List Tables and Schema' tool), ensures the generated query is valid, and then executes it using another n8n node (the 'Execute a SQL query in MySQL' tool).

This setup provides a high degree of control, accuracy, and efficiency. It demonstrates the power of integrating AI tools and core database operations within a single n8n workflow, making it an indispensable asset for modern data operations. This is a highly practical n8n workflow that showcases advanced Langchain integration.

How it Works

This process begins with the designated n8n trigger: the 'When chat message received' n8n node, which captures the user's natural language question.


  1. Trigger Initiation: The 'When chat message received' n8n trigger starts the workflow upon receiving a conversational input.

  2. AI Agent Orchestration: The input is passed to the 'AI Agent' n8n node. This agent is configured with specific instructions to act as a helpful internal assistant, prioritizing tool use over guessing.

  3. Schema Check (First Tool Use): Before attempting any data retrieval, the AI Agent intelligently calls the 'SQL DB - List Tables and Schema' n8n node. This tool provides the agent with current, accurate table and column information.

  4. Query Generation: The 'OpenAI Chat Model' (using GPT-4.1-mini) receives the user query, the schema data, and the agent's instructions. It generates the necessary SQL query.

  5. Query Execution (Second Tool Use): The AI Agent then utilizes the 'Execute a SQL query in MySQL' n8n node to run the generated SQL against the database.

  6. Response Generation: The query results are passed back to the AI Agent, which uses the conversational 'Simple Memory' n8n node to maintain context and then formats the final, clear, and professional response in markdown before returning it to the chat interface. This entire sequence is managed efficiently by the core n8n workflow structure.

Installation Guide

To deploy this powerful n8n workflow template, follow these steps:


  1. Import: Copy the entire provided n8n workflow JSON and import it into your n8n instance.

  2. OpenAI Credential Setup: Navigate to the 'OpenAI Chat Model' n8n node. Add your OpenAI API Key credentials. The system is configured to use the gpt-4.1-mini model, but you can adjust this.

  3. MySQL Credential Setup: You must set up MySQL credentials in two separate n8n node instances:

'SQL DB - List Tables and Schema'
'Execute a SQL query in MySQL'
Ensure both nodes use the same credentials pointing to your target MySQL database.

  1. Database Name Update: Crucially, open the 'SQL DB - List Tables and Schema' n8n node parameters. Locate the SQL query field and replace the placeholder yourdatabasename with the actual name of your database.

  2. Activate: Save the n8n workflow and activate it to enable the 'When chat message received' n8n trigger. You can now test the n8n workflow by sending a chat message to the designated webhook.

Node Details

This n8n workflow relies on a few key specialized n8n node types:

When chat message received (Langchain ChatTrigger): This is the initiating n8n trigger. It listens for incoming chat messages (often via a connected chat interface or webhook) to start the natural language query process.
AI Agent (Langchain Agent): The brain of the n8n workflow. It orchestrates tool use, deciding when to check the schema, when to execute a query, and how to formulate the final answer based on strict instructions.
OpenAI Chat Model (Langchain LLM): Supplies the language generation capability, translating the user's need and the schema into precise SQL commands. It uses credentials linked to your OpenAI account.
Simple Memory (Langchain Memory): A critical n8n node for maintaining conversation context. It allows the agent to recall the previous five messages, ensuring follow-up questions are understood within the context of the running n8n workflow session.
SQL DB - List Tables and Schema (MySQL Tool n8n node): This tool provides introspection. It runs a query to list the available tables and columns, feeding this vital context to the AI before query generation. This dramatically improves the accuracy of the generated SQL in this n8n workflow.
Execute a SQL query in MySQL (MySQL Tool n8n node): This final action n8n node executes the SQL code generated by the AI Agent and returns the resulting data set.

Related n8n Workflows

Free

Nodes: 6 Nodes
Updated: December 26 2025
View all
Created by
Moe Ahad
Moe Ahad

Featured*