Automate SQL Data Visualization with n8n, OpenAI & Quickchart

Discover how this unique n8n workflow empowers business analysts like Emma to transform SQL database queries into easy-to-understand interactive charts using OpenAI and Quickchart.io, saving hours and improving insights.
lmChatOpenAi
agent
httpRequest
+6
Workflow Identifier: 1153
NODES in Use: lmChatOpenAi, executeWorkflow, executeWorkflowTrigger, httpRequest, set, chatTrigger, agent, memoryBufferWindow, textClassifier
Automate SQL data with n8n and OpenAI

Press CTRL+F5 if the workflow didn't load.

Learn how to Build this Workflow with AI:

What This Automation Does

This workflow takes a user’s normal question about coffee sales data and turns that into a database query and an answer fast.

It solves the problem of writing SQL queries and making charts by hand, which takes a lot of time and can have mistakes.

The user gets a clear answer text, and if helpful, a chart image to understand sales trends quickly.


How This Workflow Works

Inputs

The main input is a chat message where the user types a question in normal language.

Processing Steps

  • Extract user question: The workflow cleans the chat input to get the exact question for SQL query.
  • Generate SQL query: An AI Agent uses the question to create a safe PostgreSQL SELECT query and runs it.
  • Classify output type: Another AI model checks if results should show a chart or just text.
  • If chart required: A sub-workflow calls OpenAI to create a Chart.js definition JSON based on the SQL results.
  • Create chart image: The chart JSON is put into a Quickchart.io URL to make a chart image link.
  • Combine and send response: The final reply merges the text answer with the chart graphic if needed.

Output

The output is a message with answered text and an optional chart image URL.


Who Should Use This Workflow

Anyone who needs to get data answers from SQL databases but does not want to write SQL manually can use it.

This is good for beginners or data people who want quick charts and answers from their data.


Tools and Services Used

  • n8n automation platform: Runs the workflow and connects the nodes.
  • PostgreSQL database: Contains the coffee sales data and runs SQL queries.
  • OpenAI GPT API: Generates SQL queries, classifies whether a chart is needed, and creates Chart.js JSON.
  • Quickchart.io: Turns Chart.js JSON into a chart image URL.

Beginner Step-by-Step: Using This Workflow in n8n Production

Download and Import

  1. Click the Download button on this page to get the workflow file.
  2. Open your n8n editor.
  3. Use the Import from File option to upload the downloaded workflow.

Configure Credentials

  1. Add your PostgreSQL database credentials in the AI Agent node settings.
  2. Enter your OpenAI API Key in the OpenAI Chat Model and classification nodes.
  3. If using different databases or channels, update the respective node parameters, like table names.

Update IDs and Prompts

  1. Check any IDs for sub-workflows and ensure they match with imported workflows.
  2. Make sure input prompts or query prefix strings are as you want them; copy and paste code snippets if provided.

Test the Workflow

  1. Send a test chat message to your webhook URL to confirm the workflow triggers.
  2. Review output in execution logs to see the answer text and chart if shown.

Activate for Production

  1. Switch the workflow from inactive to active in n8n.
  2. Share the webhook URL with users who will send data questions.
  3. Monitor workflow runs in n8n to catch any errors and fix them.
  4. If using self-host n8n, check self-host n8n for hosting help.


Inputs and Outputs

  • Input: Chat message text with user question about coffee sales data.
  • Output: Text answer describing sales insights.
  • Optional Output: Chart image URL illustrating results if useful.

Handling Errors and Special Cases

  • SQL syntax errors: Check the AI Agent prompt and database permissions to fix query problems.
  • Chart does not render: Validate Chart.js JSON and simplify if Quickchart errors happen.
  • No workflow output: Confirm webhook settings and node connections are correct.

Customization Ideas

  • Change SQL prompt in AI Agent to support other databases like MySQL.
  • Adjust Quickchart URL to make bigger or smaller charts.
  • Add new classification options to decide between charts and tables.
  • Update OpenAI nodes to new GPT versions as they come out.

Summary

✓ Get fast data answers and charts from natural language questions.

✓ Avoid manual SQL writing and chart creation.

→ Save time and reduce mistakes in reporting.

→ Use simple chat questions to unlock data insights instantly.


Automate SQL data with n8n and OpenAI

Visit through Desktop to Interact with the Workflow.

Frequently Asked Questions

Yes, by changing the AI Agent prompt and database credentials, the workflow supports MySQL.
The OpenAI Chat Model Classifier node uses a prompt to classify if the answer requires a chart or just text.
Problems usually come from invalid Chart.js JSON or Quickchart URL errors; verify JSON output to fix.
Download the workflow file, import it into n8n, add API keys and database credentials, test it, then activate.

Promoted by BULDRR AI

Related Workflows

Automate Twist Channel Creation and Messaging with n8n

This workflow automates creating and updating a channel in Twist and sending a personalized message to specific users. It eliminates manual setup errors and saves time managing Twist communications.

Automate Ideogram Image Generation with Google Sheets & Gmail

This workflow automates graphic design image generation via Ideogram AI, storing image data in Google Sheets and Google Drive, with email alerts via Gmail. It saves designers hours by automating image creation, remixing, review, and record-keeping.

Automate IT Support with Slack and OpenAI in n8n

Streamline IT support by automating Slack message handling using n8n and OpenAI. This workflow handles Slack DMs, filters bots, queries a Confluence knowledge base, and delivers AI-generated responses, improving support efficiency and response time.

Automate Crypto Analysis with CoinMarketCap & n8n AI Agent

Discover how this unique n8n workflow leverages CoinMarketCap’s multi-agent AI to deliver precise, real-time cryptocurrency insights directly via Telegram. Manage crypto data analysis efficiently with automated multi-source API integration.

Automate Gumroad to Beehiiv Subscriber Sync with n8n

Learn how to automatically add new Gumroad sales customers as Beehiiv newsletter subscribers using n8n automation. This workflow saves time by syncing sales data to Google Sheets CRM and notifying your Telegram channel instantly.

Generate On-Brand Blog Articles Using n8n and OpenAI

This workflow automates the creation of on-brand blog articles by analyzing existing company content using n8n and OpenAI. It extracts article structures and brand voice to produce consistent draft articles, saving significant content creation time.
1:1 Free Strategy Session
Your competitors are already automating. Are you still paying for it manually?

Do you want to adopt AI Automation?

Every hour your team does repetitive work, you're burning real money.
While you wait, faster businesses are cutting costs and moving quicker.
AI and automations aren't the future anymore — they're the present.

Book a live 1-on-1 session where we show you exactly which of your daily tasks can be automated — and what it’s costing you not to.