# Email assistant: convert natural language to SQL queries: n8n workflow

> Started by someone opening a chat with it, and works across Postgres. It hands the judgement calls to a local model.

Source: https://millionrounds.com/workflow/email-assistant-convert-natural-language-to-sql-queries (Millionrounds; every figure on the page links to its filing or article)

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

# Email assistant: convert natural language to SQL queries

Started by someone opening a chat with it, and works across Postgres. It hands the judgement calls to a local model. 23 steps, 4 branches: large, and worth reading before you trust it with anything live.

Large Calls a model: a local model

[Download free](https://millionrounds.com/login?next=%2Fapp%2Fworkflow%2Femail-assistant-convert-natural-language-to-sql-queries)

**23 steps** Large: read it before trusting it with anything live

**4** Branches: places the flow splits

**0** Loops or waits

**1** Service it connects to

## The flow

Starts it 3 Model 2 Logic 11 Service 7

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

- 1 **Chat Trigger** Chat trigger *Starts it*
- 2 **When clicking "Test workflow"** Manual trigger *Starts it*
- 3 **Ollama Chat Model** Lm Chat Ollama *Model*
- 4 **WorkflowTrigger** Execute Workflow trigger *Starts it*
- 5 **Load the schema from the local file** Read Write File *Service*
- 6 **List all tables in a database** Postgres *Service*
- 7 **AI Agent** Agent *Model*
- 8 **If file exists or already retried generating it** If *Logic*
- 9 **List all columns in a table** Postgres *Service*
- 10 **Extract SQL query** Set *Logic*
- 11 **Extract data from file** Extract From File *Service*
- 12 **Add table name to output** Set *Logic*
- 13 **Check for trailing semicolon** If *Logic*
- 14 **Combine schema data and chat input** Set *Logic*
- 15 **Convert data to binary** Convert To File *Service*
- 16 **Add trailing semicolon** Set *Logic*
- 17 **Check if query exists** If *Logic*
- 18 **Save file locally** Read Write File *Service*
- 19 **Combine query result and chat answer** Merge *Logic*
- 20 **Postgres** Postgres *Service*
- 21 **Format empty output** Set *Logic*
- 22 **If ran manually** If *Logic*
- 23 **Format query results** Set *Logic*

## How it's built

- **Starts on:** Someone opening a chat with it
- **Steps:** 23, with 4 branches
- **Size:** Large: read it before trusting it with anything live
- **Connects to:** Postgres
- **Model:** a local model

## Questions

What does it need to run?

An n8n account, plus your own Postgres account, and a key for a local model. 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 a local model. Model calls are billed by the provider, not by n8n.

How long does it take to set up?

Take your time. At 23 steps it is large: read it before trusting it with anything live.

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 23 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%2Femail-assistant-convert-natural-language-to-sql-queries)
- **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.

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

## Workflows like it

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

- [**Auto-generate & publish SEO articles to WordPress with OpenAI** 23 steps · calls a model · Postgres](https://millionrounds.com/workflow/auto-generate-and-publish-seo-articles-to-wordpress-with-ope)
- [**Automate blog-to-social media with OpenAI and Reddit** 24 steps · calls a model · LinkedIn, Postgres](https://millionrounds.com/workflow/automate-blog-to-social-media-with-openai-and-reddit)
- [**Generate Gmail reply drafts with OpenAI and Postgres** 22 steps · calls a model · Gmail, Postgres](https://millionrounds.com/workflow/generate-gmail-reply-drafts-with-openai-and-postgres)
- [**Collect WhatsApp questionnaire responses in Postgres** 22 steps · Postgres, WhatsApp](https://millionrounds.com/workflow/collect-whatsapp-questionnaire-responses-in-postgres)
- [**Viral content opportunities from Twitter, Reddit** 24 steps · Email, Postgres](https://millionrounds.com/workflow/viral-content-opportunities-from-twitter-reddit)
- [**Gate pgvector indexing by checking consent in Postgres** 21 steps · calls a model · Postgres, Slack](https://millionrounds.com/workflow/gate-pgvector-indexing-by-checking-consent-in-postgres)
