Enterprise-Grade Text-to-SQL with n8n — Part 1: From User Query to SQL Generation

Berke Azal20 Jan 2026

Problem

Trendyol's dashboard platform covers the standard views, such as daily sales for Turkey. Users often need something outside them, for example one brand's performance yesterday or a comparison nobody built. A new dashboard for every ad-hoc request does not scale. Connecting an LLM directly to the warehouse brings two risks: security and hallucination. Without routing, even "Hello" would start a paid BigQuery job.

Example from the article: a user asks "What was yesterday's revenue?" and gets SQL-backed numbers, while "Tell me tomorrow's weather" gets a polite refusal that lists the supported topics.

Solution in brief

Put a cheap classifier at the entry that splits chit-chat from data requests. Send data requests to an SQL-generating agent whose system prompt carries a column dictionary with business meanings, hard-coded formulas, fixed date expressions and 10+ verified question and SQL pairs. The agent queries a dedicated flat 30-day table, not the live dashboards' schema.

Questions this case answers
  • How to generate correct SQL from free-text questions when business metrics have non-obvious formulas and relative dates must match the data load schedule
  • How to route incoming messages so that only genuine data requests reach the costly query-generation path
  • How to let business users ask ad-hoc questions in natural language and get correct SQL answers from a warehouse?
  • How to stop an LLM from inventing metrics or misreading business definitions when it writes SQL?

Tools and methods

  • n8n — orchestrates the branched workflow: classifier, chit-chat agent, SQL agent, validation, response
  • Azure OpenAI — the model behind the classifier and the agents
  • BigQuery — warehouse that holds the data mart and runs the generated SQL
  • LLM intent classification — two-way router: ChitChat or Analyze
  • Few-shot prompting — 10+ verified question and SQL pairs inside the SQL prompt
  • Text-to-SQL — context-aware prompt with schema dictionary and business rules
Continue reading — it's free

Google or a one-time email code. No password.