AI-Powered Conversational SQL Query Generator - n8n Workflow

Use this advanced n8n workflow to generate complex SQL queries from natural language requests based only on your database schema, powered by GPT-4o. Ideal n8n templates for developers.

Workflow Preview

Ready to automate?

Download this n8n workflow template and start using it instantly.

Who is this best for?


  • Database Administrators (DBAs) and Developers seeking rapid, conversational query generation.

  • Users looking for advanced n8n templates utilizing the LangChain AI Agent capabilities.

  • Automation specialists who need a robust n8n workflow for interacting with SQL databases using LLMs.

Overview

Navigating complex database schemas and writing custom SQL can be time-consuming. This sophisticated n8n workflow solves this by providing an AI-powered conversational interface. The core value of this n8n automation is its ability to operate purely on the database schema, meaning the AI Agent never requires access to sensitive production data.

This setup leverages the power of the OpenAI n8n node (GPT-4o) combined with LangChain nodes, ensuring that when a user inputs a question (e.g., 'Show me all German customers'), the AI Agent generates the precise SQL query. This specific n8n workflow is highly efficient because it pre-loads the schema locally, preventing repetitive, slow calls to the remote database for every interaction. This is one of the most practical n8n templates for integrating AI agents with legacy data sources.

How it Works

This n8n workflow operates in two phases: setup and runtime.

Phase 1: Initial Setup (Schema Extraction)


  1. The 'When clicking "Test workflow"' manual n8n trigger initiates the process.

  2. The MySQL n8n node ('List all tables in a database') fetches all table names.

  3. For each table, the subsequent MySQL n8n node ('Extract database schema') runs a DESCRIBE command.

  4. The 'Add table name to output' Set n8n node combines the schema details with the relevant table name.

  5. The 'Convert data to binary' n8n node converts this structure to JSON, which is saved locally via the 'Save file locally' n8n node as ./chinook_mysql.json.

Phase 2: Runtime (Per Chat Message)


  1. The 'Chat Trigger' n8n trigger activates upon receiving a user message.

  2. The workflow reads and extracts the pre-saved schema data from the local file using two ReadWriteFile and ExtractFromFile n8n nodes.

  3. The 'Combine schema data and chat input' Set n8n node packages the schema and the user's question for the AI Agent.

  4. The core 'AI Agent' n8n node utilizes the 'OpenAI Chat Model' (GPT-4o) and 'Window Buffer Memory' to maintain conversation context. It processes the schema and request, generating an SQL query if necessary, guided by a specialized system prompt that instructs it not to execute the query itself.

  5. The 'Extract SQL query' Set n8n node uses a regular expression to identify and isolate the SQL command from the Agent's output.

  6. The 'Check if query exists' If n8n node determines if an SQL command was found.

  7. If Query Exists: The 'Run SQL query' MySQL n8n node executes the query. Results are then processed and formatted by the 'Format query results' n8n node and combined with the Agent's descriptive response.

  8. If No Query Exists (or failure): The flow bypasses the database execution, and the Agent's immediate text answer is passed directly to the chat interface via a No Operation n8n node pathway.

Installation Guide

To install this n8n workflow, follow these steps:


  1. Import the n8n workflow: Copy the provided JSON code and import it into your n8n instance via the 'Workflows' section using the 'New' -> 'Import from JSON' option.

  2. Configure Credentials: You will need two types of credentials:

OpenAI API: Update the 'OpenAI Chat Model' n8n node with your OpenAI API key credentials (must support gpt-4o).
MySQL Database: Update the MySQL n8n nodes ('List all tables...', 'Extract database schema', 'Run SQL query') with connection details for your database instance. The initial setup assumes a database like db4free is used.

  1. Run Initial Setup: Execute the 'When clicking "Test workflow"' n8n trigger once (click 'Test workflow' in the editor) to extract your database schema and save it to the local file system where n8n is running. This file is crucial for the subsequent chat operations.

  2. Activate Chat: Activate the workflow. You can now interact with the 'Chat Trigger' n8n node URL directly or embed it in a front-end application.

Node Details

Chat Trigger (n8n trigger): Initiates the main AI sequence upon receiving user chat input, providing the prompt to the Agent.
MySQL n8n node (List all tables/Run SQL query): Handles interaction with the MySQL database. It is used both in the setup phase (schema extraction) and the runtime phase (query execution).
ReadWriteFile n8n node: Used to persistently store the database schema as a JSON file locally (./chinook_mysql.json), improving speed and efficiency for every subsequent run of the n8n workflow.
OpenAI Chat Model (LangChain n8n node): The underlying LLM, configured here to use gpt-4o with a low temperature for reliable SQL generation.
AI Agent (LangChain n8n node): The brain of the operation. It manages the conversation, utilizes the schema data provided, and generates appropriate conversational or SQL responses. Its system prompt is meticulously configured to instruct it on query formatting and tool usage.
Window Buffer Memory (LangChain n8n node): Essential for conversational n8n templates, this node stores the last 10 conversation turns, allowing the AI Agent to remember context within the n8n workflow session.


  • Set n8n node (Extract SQL query): Crucial for safety and execution control. It uses a regular expression to extract only the generated SQL query, preventing the AI Agent from executing potentially harmful commands.

Related n8n Workflows

Free

Nodes: 14 Nodes
Updated: December 26 2025
View all
Created by

Let's connect!

Featured*