Generate SQL queries from schema only - AI-powered n8n Workflow Template

n8n workflow template · 19 nodes · OpenAI

Generate SQL queries from schema only - AI-powered is a free n8n workflow template that starts when a chat message is received and works with OpenAI and MySQL. It has 19 nodes, 4 of them AI nodes (OpenAI Chat Model, Window Buffer Memory, Chat Trigger and AI Agent). The notes inside the workflow say: “Now you can use chat to "talk" to your data!” To run it, you add your own credentials for MySQL and OpenAI API.

At a glance

Trigger
Chat Trigger (Chat Trigger)
Nodes
19
Apps
OpenAI and MySQL
AI nodes
4
Credentials
2 types
Category
Spreadsheets, databases and docs

Apps and integrations

  • OpenAI
  • MySQL

Node types used

  • OpenAI Chat Model
  • Window Buffer Memory
  • MySQL
  • Edit Fields (Set)
  • Convert to File
  • Read/Write Files from Disk
  • Extract from File
  • Chat Trigger
  • AI Agent
  • Manual Trigger
  • If
  • Merge

How it works

The text below comes from the sticky notes inside the workflow. We removed links, code samples and anything that looks like a key or token.

Run this part only once

  • loads a list of all tables from the database hosted on db4free
  • extracts the database schema for each table and adds the table name
  • converts the schema into a binary JSON format
  • saves the schema./chinook_mysql.json file locally

Now you can use chat to "talk" to your data!

On every chat message

  • The workflow gets the data from the local schema file and extracts it as a JSON object. This way, we achieve two important improvements
  • faster processing time as we don't need to fetch the schema for each table from a slow remote database
  • the Agent will know database structure without seeing the actual data
  • DB schema is then converted into a long string, JSON fields from the Chat Trigger are added before they are entered into the Agent node.

LangChain AI Agent's system prompt is modified.

It uses only the database schema to generate SQL queries. The agent creates these queries but does not execute them. Instead, it passes them to subsequent nodes.

Example: "Can you show me the list of all German customers?"

Queries are generated only when necessary; for some requests, a query may not be needed. This is because certain questions can be answered directly without SQL execution.

Example: "Can you list me all tables?"

The AI Agent remembers the schema, questions, and final answers, but not data values, since queries run externally. The agent can't access database content.

SQL query extraction

Check if the agent's response contains an SQL query. If it does, we extract the query using a regular expression.

  • The SQL node accesses the database and executes the query. The results are then formatted for readability.
  • Both the chat response and the query result are displayed in the chat window.

When the agent responds without an SQL query, you receive an immediate answer with no additional processing.

Setup notes

The text below comes from the sticky notes inside the workflow. We removed links, code samples and anything that looks like a key or token.

Pre-workflow setup

Connect to a free MySQL server and import your database. Follow Step 1 and 2 in this tutorial for more.

The Chinook data used in this workflow is available on GitHub.

Nodes in this workflow

The 19 nodes in file order (sticky notes excluded). Branches and connections determine execution order in n8n:

  1. OpenAI Chat Model (OpenAI Chat Model)
  2. Window Buffer Memory (Window Buffer Memory)
  3. List all tables in a database (MySQL)
  4. Extract database schema (MySQL)
  5. Add table name to output (Edit Fields (Set))
  6. Convert data to binary (Convert to File)
  7. Save file locally (Read/Write Files from Disk)
  8. Extract data from file (Extract from File)
  9. Chat Trigger (Chat Trigger)
  10. AI Agent (AI Agent)
  11. When clicking "Test workflow" (Manual Trigger)
  12. Combine schema data and chat input (Edit Fields (Set))
  13. Load the schema from the local file (Read/Write Files from Disk)
  14. Extract SQL query (Edit Fields (Set))
  15. Check if query exists (If)
  16. Format query results (Edit Fields (Set))
  17. Run SQL query (MySQL)
  18. Prepare final output (Edit Fields (Set))
  19. Combine query result and chat answer (Merge)

Credentials you need

  • MySQL – used by Extract database schema, List all tables in a database and Run SQL query
  • OpenAI API – used by OpenAI Chat Model

Configure your own credentials after importing. Create each credential in n8n, then select it in the node.

How to import into n8n

  1. Download the JSON file with the button above.
  2. In n8n, open a new workflow, select the ... menu and choose Import from File. You can also paste the JSON straight into the editor canvas.
  3. Open each node that shows a warning and select or create its credential.
  4. Read the notes on the canvas, test the workflow with sample data, then activate it.

Do you need somewhere to run it? Read our guide to self-hosted n8n.

Need this customised?

We can adapt this workflow to your apps, data and process, then deploy and support it.

Contact Us