AI SQL Generator Workflow in n8n | Using Ollama (Qwen 2.5 Model) + Chat with Your Database
- ARUN .N.K
- Aug 22
- 2 min read
Simply chat with your database and get instant insights — not only the executed results but also the actual SQL query behind it. 🚀
In this post, I’ll walk you through how to build an AI-powered SQL Generator workflow in n8n that:
Takes natural language input from a chat
Generates an SQL query using AI
Executes the query against your database
Returns both the SQL query and results
N8N WORKFLOW

Why This Workflow?
Most people don’t memorize database schemas. Writing SQL queries manually can be time-consuming. This workflow solves that by:
✅ Loading your schema automatically
✅ Using AI to generate SQL
✅ Executing the query in MySQL
✅ Returning both SQL + executed results in one reply Database Setup: BikeStore (MySQL)
In this setup, we’re using the BikeStore sample database running on MySQL.
The schema includes key tables for:
Customers, Orders & Order Items
Stores & Staffs
Products, Brands & Categories
Stocks
Here’s the ERD:
URL : https://www.kaggle.com/datasets/dillonmyrick/bike-store-sample-database

Step-by-Step Workflow in n8n
Step 1. Extract & Save MySQL Schema (One-Time Setup)
This ensures AI knows your database structure.
Connect to your MySQL BikeStore database.
List all tables.
Extract schema details (columns, relationships).
Save the schema locally as a .json file.
📌 In n8n:
Use Execute Query node → SHOW TABLES;
Extract schema with another query: DESCRIBE TABLE table_name;
Save JSON schema with Write File node.
Step 2. Handle Incoming Chat Message
Whenever a new chat input arrives:
Load the saved schema file.
Combine schema + chat message.
Send to the SQL AI Agent (Qwen 2.5 running in Ollama).
Step 3. AI SQL Agent Workflow
Inside n8n, the workflow:
Extract SQL → Parse SQL from AI response.
Check if query exists → Skip if none.
Run SQL → Execute against BikeStore DB.
Format results → Convert into human-readable table.
Combine SQL + results → Merge into one reply.
Return to chat.
Example with BikeStore
👉 User asks: calculate the total sales for each brand across all stores
AI generates SQL:
SELECT b.brand_name, SUM(oi.list_price * oi.quantity) AS total_sales
FROM order_items oi
JOIN products p ON oi.product_id = p.product_id
JOIN brands b ON p.brand_id = b.brand_id
GROUP BY b.brand_name
ORDER BY total_sales DESC;
SQL result:
brand_name | total_sales
Trek | 4369534.36
Electra | 1137616.62
Surly | 973402.30
Sun Bicycles | 346782.30
Haro | 183397.03
Heller | 178118.75
Pure Cycles | 159160.00
Ritchey | 87748.83
Strider | 2249.87
More Examples :
Why Qwen 2.5 with Ollama?
Qwen 2.5 is strong in structured reasoning & SQL generation.
Running with Ollama makes deployment lightweight & local.
Produces clean SQL with fewer hallucinations compared to generic LLMs.
Final Thoughts
This AI SQL Generator workflow in n8n lets you:
Ask database questions in plain English
Get both SQL query + results back instantly
Build transparent AI-powered analytics
With n8n + Ollama + BikeStore DB, you now have your own SQL assistant chatbot! 🎉






Comments