Skip to main content

Generate SQL queries from schema only - AI-powered

Workflow preview

Workflow preview
100%
Generate SQL queries from schema only - AI-powered preview
Open on n8n.io

Important notice

This workflow is provided as-is. Please review and test before using in production.

1. Workflow Overview

This workflow is a modification of the previous template on how to [create an SQL agent with LangChain and SQLite](https://n8n.io/workflows/2292 talk to your sqlite database with a langchain ai age...

Best for

  • Engineering automation workflows
  • AI Chatbot automation workflows
  • advanced n8n builders looking for reusable templates

Tools used

@n8n/n8n-nodes-langchain.lmchatopenai, @n8n/n8n-nodes-langchain.memorybufferwindow, n8n-nodes-base.noop, n8n-nodes-base.mysql, n8n-nodes-base.set, n8n-nodes-base.converttofile, n8n-nodes-base.readwritefile, n8n-nodes-base.extractfromfile

Source and attribution

This workflow is cataloged by N8N Workflows and links back to its original n8n.io source page by Yulia.

Original n8n.io source

1.1 Workflow description

Title
Generate SQL queries from schema only - AI-powered
Workflow name
Generate SQL queries from schema only - AI-powered

This workflow is a modification of the previous template on how to create an SQL agent with LangChain and SQLite.

The key difference – the agent has access only to the database schema, not to the actual data. To achieve this, SQL queries are made outside the AI Agent node, and the results are never passed back to the agent.

This approach allows the agent to generate SQL queries based on the structure of tables and their relationships, without having to access the actual data.

This makes the process more secure and efficient, especially in cases where data confidentiality is crucial.

🚀 Setup

To get started with this workflow, you’ll need to set up a free MySQL server and import your database (check Step 1 and 2 in this tutorial).

Of course, you can switch MySQL to another SQL database such as PostgreSQL, the principle remains the same. The key is to download the schema once and save it locally to avoid repeated remote connections.

Run the top part of the workflow once to download and store the MySQL chinook database schema file on the server.

With this approach, we avoid the need to repeatedly connect to a remote db4free database and fetch the schema every time. As a result, we reach greater processing speed and efficiency.

🗣️ Chat with your data

  1. Start a chat: send a message in the chat window.
  2. The workflow loads the locally saved MySQL database schema, without having the ability to touch the actual data. The file contains the full structure of your MySQL database for analysis.
  3. The Langchain AI Agent receives the schema, your input and begins to work.
  4. The AI Agent generates SQL queries and brief comments based solely on the schema and the user’s message.
  5. An IF node checks whether the AI Agent has generated a query. When:
  • Yes: the AI Agent passes the SQL query to the next MySQL node for execution.
  • No: You get a direct answer from the Agent without further action.
  1. The workflow formats the results of the SQL query, ensuring they are convenient to read and easy to understand.
  2. Once formatted, you get both the Agent answer and the query result in the chat window.

🌟 Example queries

Try these sample queries to see the schema-driven AI Agent in action:

  1. Would you please list me all customers from Germany?

  2. What are the music genres in the database?

  3. What tables are available in the database?

  4. Please describe the relationships between tables. - In this example, the AI Agent does not need to create the SQL query.

And if you prefer to keep the data private, you can manually execute the generated SQL query in your own environment using any database client or tool you trust 🗄️

💭 The AI Agent memory node does not store the actual data as we run SQL-queries outside the agent. It contains the database schema, user questions and the initial Agent reply. Actual SQL query results are passed to the chat window, but the values are not stored in the Agent memory.

1.2 Logical Blocks

This catalog entry is organized from the workflow JSON. The node-level section below shows the executable blocks available for review before importing the template.

2. Block-by-Block Analysis

Block 1 - OpenAI Chat Model

Type / Role
@n8n/n8n-nodes-langchain.lmChatOpenAi - lmChatOpenAi
Config choices
Version 1

Block 2 - Window Buffer Memory

Type / Role
@n8n/n8n-nodes-langchain.memoryBufferWindow - memoryBufferWindow
Config choices
Version 1.2

Block 3 - No Operation, do nothing

Type / Role
n8n-nodes-base.noOp - noOp
Config choices
Version 1

Block 4 - List all tables in a database

Type / Role
n8n-nodes-base.mySql - mySql
Config choices
Version 2.4

Block 5 - Extract database schema

Type / Role
n8n-nodes-base.mySql - mySql
Config choices
Version 2.4

Block 6 - Add table name to output

Type / Role
n8n-nodes-base.set - set
Config choices
Version 3.4

Block 7 - Convert data to binary

Type / Role
n8n-nodes-base.convertToFile - convertToFile
Config choices
Version 1.1

Block 8 - Save file locally

Type / Role
n8n-nodes-base.readWriteFile - readWriteFile
Config choices
Version 1

Block 9 - Extract data from file

Type / Role
n8n-nodes-base.extractFromFile - extractFromFile
Config choices
Version 1

Block 10 - Chat Trigger

Type / Role
@n8n/n8n-nodes-langchain.chatTrigger - chatTrigger
Config choices
Version 1.1

Block 11 - AI Agent

Type / Role
@n8n/n8n-nodes-langchain.agent - agent
Config choices
Version 1.6

Block 12 - Sticky Note

Type / Role
n8n-nodes-base.stickyNote - stickyNote
Config choices
Version 1

Block 13 - Sticky Note1

Type / Role
n8n-nodes-base.stickyNote - stickyNote
Config choices
Version 1

Block 14 - When clicking "Test workflow"

Type / Role
n8n-nodes-base.manualTrigger - manualTrigger
Config choices
Version 1

Block 15 - Sticky Note2

Type / Role
n8n-nodes-base.stickyNote - stickyNote
Config choices
Version 1

Block 16 - Sticky Note3

Type / Role
n8n-nodes-base.stickyNote - stickyNote
Config choices
Version 1

Block 17 - Combine schema data and chat input

Type / Role
n8n-nodes-base.set - set
Config choices
Version 3.4

Block 18 - Load the schema from the local file

Type / Role
n8n-nodes-base.readWriteFile - readWriteFile
Config choices
Version 1

Block 19 - Extract SQL query

Type / Role
n8n-nodes-base.set - set
Config choices
Version 3.4

Block 20 - Check if query exists

Type / Role
n8n-nodes-base.if - if
Config choices
Version 2.2

Block 21 - Sticky Note4

Type / Role
n8n-nodes-base.stickyNote - stickyNote
Config choices
Version 1

Block 22 - Sticky Note5

Type / Role
n8n-nodes-base.stickyNote - stickyNote
Config choices
Version 1

Block 23 - Sticky Note7

Type / Role
n8n-nodes-base.stickyNote - stickyNote
Config choices
Version 1

Block 24 - Sticky Note6

Type / Role
n8n-nodes-base.stickyNote - stickyNote
Config choices
Version 1

Showing the first 24 of 29 workflow blocks. Download the JSON for the full node graph.

3. Summary Table

Workflow Generate SQL queries from schema only - AI-powered
Complexity advanced
Nodes 29
Categories Engineering, AI Chatbot
Author Yulia
Published 29 Oct 2024

4. Reproducing the Workflow from Scratch

  1. 1. Download the workflow JSON

    Use the JSON export at /data/workflows/2508/2508.json as the source template for this automation.

  2. 2. Import the template into n8n

    Open n8n, import the downloaded JSON, and review each node before activating the workflow.

  3. 3. Configure credentials and variables

    Replace placeholder credentials, API keys, webhook URLs, account IDs, and environment-specific values with your own settings.

  4. 4. Test with sample data

    Run the workflow manually or in a staging workspace, inspect node output, and confirm downstream systems receive the expected data.

  5. 5. Activate and monitor

    Enable the workflow only after testing, then monitor executions, errors, and rate limits during the first production runs.

5. General Notes & Resources

Review imported nodes carefully before activation. This catalog entry is intended to help you inspect the workflow structure, understand required services, and find related templates faster.

Node names, credentials, schedules, webhook paths, and external service limits may need adjustment for your workspace.

Frequently asked questions

What does Generate SQL queries from schema only - AI-powered do?

This workflow is a modification of the previous template on how to [create an SQL agent with LangChain and SQLite](https://n8n.io/workflows/2292 talk to your sqlite database with a langchain ai age...

What do I need before importing this workflow?

Review the workflow JSON, configure any required credentials in n8n, and test the automation in a safe workspace before using it in production.

Can I customize this workflow?

Yes. Use the block-by-block analysis and the downloadable JSON to inspect each node, then adjust credentials, prompts, schedules, filters, or destinations for your Engineering, AI Chatbot use case.