# Sql queries from schema with OpenAI and MySQL: n8n workflow

> Started by someone opening a chat with it, and works across MySQL. It puts OpenAI behind a retrieval step, so answers are drawn from documents you supply...

Source: https://millionrounds.com/workflow/sql-queries-from-schema-with-openai-and-mysql (Millionrounds; every figure on the page links to its filing or article)

[Automation library](https://millionrounds.com/workflows) · [Data collection](https://millionrounds.com/workflows?category=Data+collection#library)

# Sql queries from schema with OpenAI and MySQL

Started by someone opening a chat with it, and works across MySQL. It puts OpenAI behind a retrieval step, so answers are drawn from documents you supply rather than from the model alone. 20 steps, 1 branch: a working afternoon to understand properly.

Medium Calls a model: OpenAI Retrieval

[Download free](https://millionrounds.com/login?next=%2Fapp%2Fworkflow%2Fsql-queries-from-schema-with-openai-and-mysql)

**20 steps** Medium: a working afternoon

**1** Branch: places the flow splits

**0** Loops or waits

**1** Service it connects to

## The flow

Starts it 2 Model 3 Logic 8 Service 7

The steps in the order they run, from what starts it.

- 1 **OpenAI Chat Model** Lm Chat Open Ai *Model*
- 2 **Window Buffer Memory** Memory Buffer Window *Model*
- 3 **Chat Trigger** Chat trigger *Starts it*
- 4 **When clicking "Test workflow"** Manual trigger *Starts it*
- 5 **AI Agent** Agent *Model*
- 6 **Load the schema from the local file** Read Write File *Service*
- 7 **List all tables in a database** My Sql *Service*
- 8 **Extract SQL query** Set *Logic*
- 9 **Extract data from file** Extract From File *Service*
- 10 **Extract database schema** My Sql *Service*
- 11 **Check if query exists** If *Logic*
- 12 **Combine schema data and chat input** Set *Logic*
- 13 **Add table name to output** Set *Logic*
- 14 **Run SQL query** My Sql *Service*
- 15 **Combine query result and chat answer** Merge *Logic*
- 16 **No Operation, do nothing** No Op *Logic*
- 17 **Convert data to binary** Convert To File *Service*
- 18 **Format query results** Set *Logic*
- 19 **Prepare final output** Set *Logic*
- 20 **Save file locally** Read Write File *Service*

## How it's built

- **Starts on:** Someone opening a chat with it
- **Steps:** 20, with 1 branch
- **Size:** Medium: a working afternoon
- **Connects to:** MySQL
- **Model:** OpenAI
- **Retrieval:** Answers are drawn from documents you supply, not from the model alone

## Questions

What does it need to run?

An n8n account, plus your own MySQL account, and a key for OpenAI. You connect your own accounts after importing it; none are included.

What starts it?

It starts on someone opening a chat with it.

Does it use AI?

Yes. It calls OpenAI. Its answers draw on documents you give it, not on the model alone. Model calls are billed by the provider, not by n8n.

How long does it take to set up?

Plan an afternoon. 20 steps is enough that you should read each one before running it.

How do I import it?

Copy the workflow, open a new workflow in n8n and paste. The steps appear on the canvas, ready for your own accounts.

**Before you run it**

Nothing here has been tested by us. It is somebody's working automation, read and described from its own steps. Treat the connections it wants as a list of accounts it will touch, and read the 20 steps before you point it at anything that matters.

## Run it in n8n

- **Create your n8n account** Free to start. n8n is where the workflow runs. [Create an n8n account ↗](https://millionrounds.com/n8n)
- **Download the workflow** Free with a Millionrounds account. [Download free](https://millionrounds.com/login?next=%2Fapp%2Fworkflow%2Fsql-queries-from-schema-with-openai-and-mysql)
- **Paste it into n8n** Open a new workflow in n8n and paste. Then connect your own accounts.

Stored with the author's traces removed: canvas notes, instance identifiers, saved credential names and any live document they had left behind.

## Connects to

Each opens the other workflows that use it.

[MySQL](https://millionrounds.com/workflows?service=MySQL#library)

## Workflows like it

Also data collection, with the same services where possible, closest in size first.

- [**Generate SQL queries from schema only - AI-powered with OpenAI** 20 steps · calls a model · MySQL](https://millionrounds.com/workflow/generate-sql-queries-from-schema-only-ai-powered-with-openai-2)
- [**Evaluate and rank supplier bids with MySQL and Groq LLM** 19 steps · calls a model · Gmail, MySQL](https://millionrounds.com/workflow/evaluate-and-rank-supplier-bids-with-mysql-and-groq-llm)
- [**Synchronize MySQL database schemas to Pinecone with OpenAI** 17 steps · calls a model · MySQL, Pinecone](https://millionrounds.com/workflow/synchronize-mysql-database-schemas-to-pinecone-with-openai)
- [**Sync Google Sheets data with MySQL** 13 steps · Google Sheets, MySQL](https://millionrounds.com/workflow/sync-google-sheets-data-with-mysql)
- [**Employee onboarding & offboarding with MySQL** 12 steps · Email, MySQL](https://millionrounds.com/workflow/employee-onboarding-and-offboarding-with-mysql)
- [**Sql to XML export with XSL** 11 steps · MySQL](https://millionrounds.com/workflow/sql-to-xml-export-with-xsl)
