← Back to blog

2026-05-20 · 8 min read

Natural language to SQL: the Slack bot pattern that stops stakeholders DM-ing you

A small n8n + Gemini Slack bot that answers ad-hoc data questions before they hit your DMs. Plus the clarify gate that stops garbage queries.

Close-up of smartphone showing a Slack chat with a data answer, purple LED accent

Three Mondays in a row, the same VP DM'd me with "quick question, can you pull..." that was never quick. The third Monday I counted: 14 minutes to context-switch out of what I was doing, run the query, format the answer, send it. Multiply by 6 stakeholders. 90 minutes a day of pure interrupt cost. I do not have 90 minutes a day to give away.

So I built a Slack bot in 4 hours. Plain English in, SQL run against our warehouse, answer back in the thread. One clarifying question if needed. Otherwise zero ceremony. 7 weeks in. Stakeholder DMs are down 70%. My focus time is back. The VP has not noticed.

Here is the exact pattern. The hard part is not the SQL generation. It is the clarify gate.

The stack

  • n8n cloud (one workflow, ~12 nodes)
  • Slack workspace + a Slack app with bot scopes (chat:write, app_mentions:read)
  • Postgres warehouse (read-only credentials, please)
  • Gemini 2.5 Flash via Google AI Studio (free)

The flow at 30,000 feet

  1. Stakeholder writes "@DataBot how many active users last week vs the week before in Mumbai" in #data-questions.
  2. Slack webhook fires the n8n workflow.
  3. First Gemini node decides: is this question clear enough to write SQL for? If no, post a clarifying question back in the thread (STOP or CLARIFY response). If yes, continue.
  4. Second Gemini node writes the SQL against your schema. The schema description is in the prompt.
  5. Postgres node runs the SQL with a 30s timeout.
  6. Third Gemini node turns the result into one English sentence.
  7. Slack node posts the answer in the same thread.

The clarify gate is the whole game

Every NL2SQL demo I have seen skips this step. They assume the question is clear and run the SQL. In real life, half the questions are ambiguous. "How many users?" Active users or signups? In what window? In what city? If the bot guesses, it returns confident garbage. Three confident garbage answers and your stakeholders stop trusting it. Once they stop trusting it, you cannot get them back.

My clarify gate is a single Gemini call with this prompt template:

  • "You are a data analyst assistant. Read the question. If the time window, metric, or filter is unclear, reply with CLARIFY: <one short clarifying question>. If it is clear, reply with PROCEED. Reply with nothing else."

An IF node routes CLARIFY back to Slack and stops the workflow. PROCEED continues to SQL generation. The bot asks at most one follow-up. If the human ignores it, the bot does nothing. No silent guessing.

4 design choices that mattered

1. Read-only Postgres credentials

Non-negotiable. The bot will generate SQL you did not write. You do not want it touching production tables in write mode. Spin up a separate read-only role, give it SELECT on the analytics schema only, point n8n at that.

2. Schema in the prompt, not in a vector DB

For under 20 tables, paste the schema (table names, column names, types, one-line description per table) into the system prompt. No RAG, no embeddings, no Pinecone. Cheaper, faster, easier to debug. Above 20 tables, then think about retrieval.

3. Thread the reply

Always post the answer in the same Slack thread as the question. Two reasons. One: keeps the channel scannable. Two: gives you a free audit trail of "what question got what answer" without building a logging UI.

4. 30-second SQL timeout

If the bot writes a runaway query, you want it killed. n8n's Postgres node has a timeout setting. Set it to 30 seconds. If a query takes longer, it is probably wrong anyway.

What I would not do

  • Let the bot write to the database. Read-only forever.
  • Trust the SQL without a glance. The Slack node also posts the generated SQL in a code block under the answer. Stakeholders glance, you glance, anyone can flag if it looks wrong.
  • Build a fancy NLU layer in front. Gemini Flash IS the NLU layer. One model, three calls, done.

The dashboard that didn't need to exist

My company asked me last year for a self-serve dashboard so stakeholders could answer their own questions. We scoped 6 weeks of work. We never built it. The bot covers 80% of those questions with 4 hours of build time and ₹0 of platform cost. The 6-week dashboard is still in the backlog. Nobody has asked for it once.

The remaining 20% are the questions where stakeholders actually need to talk to an analyst. Which is the conversation worth having anyway.

Want to build this with your data?

I teach this exact bot, point-to-point with your own warehouse, in the 3-Hour Champion Sprint. You leave with the working workflow, the schema prompt template, and the clarify-gate logic running against a real Postgres database. ₹1,499 Early-Bird. Saturday cohorts.

Free guide

The 6 AI prompts BI analysts actually use

The exact prompt templates BI analysts open 3x a week. Stakeholder rebuttals, SQL debugging, anomaly investigation, slide narratives. Free guide, no card, no spam.