π Generate BigQuery SQL from natural language queries using GPT-4o chat
β‘ 2,320 views Β· π Internal Wiki & Knowledge Base
Description
Give business users a chat box; get back valid BigQuery SQL and live query results.
The workflow:
- Captures a plain-language question from a chat widget or internal portal.
- Fetches the current table + column schema from your BigQuery dataset (via
INFORMATION_SCHEMA). - Feeds both the schema and the question to GPT-4o so it can craft a syntactically correct SQL query using only fields that truly exist.
- Executes the AI-generated SQL in BigQuery and returns the results.
- Stores a short-term memory by session, enabling natural follow-up questions.
Perfect for analysts, customer-success teams, or any stakeholder who needs data without writing SQL.
βοΈ Setup Instructions
-
Import the workflow
- n8n β Workflows β Import from File (or Paste JSON) β Save
-
Add credentials
Service Where to create credentials Node(s) to update OpenAI <https://platform.openai.com> β Create API key OpenAI Chat Model Google BigQuery Google Cloud Console β IAM & Admin β Service Account JSON key Google BigQuery (schema + query) -
Point the schema fetcher to your dataset
- In Google BigQuery1 youβll see:
SELECT table_name, column_name, data_type FROM `n8nautomation-453001.email_leads_schema.INFORMATION_SCHEMA.COLUMNS` - Replace
n8nautomation-453001.email_leads_schemawithYOUR_PROJECT.YOUR_DATASET. - Keep the rest of the query the sameβBigQueryβs
INFORMATION_SCHEMAalways surfacestable_name,column_name, anddata_type.
- In Google BigQuery1 youβll see:
-
Update the execution node
- Open Google BigQuery (the second BigQuery node).
- In Project ID select your project.
- The SQL Query field is already
{{ $json.output.query }}so it will run whatever the AI returns.
-
(Optional)Embed the chat interface
-
Test end-to-end
- Open the embedded chat widget.
- Ask: βHow many distinct email leads were created last week?β
- After a few seconds the workflow will return a table of resultsβor an error if the schema lacks the requested fields.
- As specific questions about your data
-
Activate
- Toggle Active so the chat assistant is available 24/7.
π§© Customization Ideas
- Row-limit safeguard: automatically append
LIMIT 1000to every query. - Chart rendering: send query results to Google Sheets + Looker Studio for instant visuals.
- Slack bot: forward both the question and the SQL result to a Slack channel for team visibility.
- Schema caching: store the INFORMATION_SCHEMA result for 24 hours to cut BigQuery costs.
Contact
- Email: rbreen@ynteractive.com
- Website: https://ynteractive.com
- YouTube: https://www.youtube.com/@ynteractivetraining
- LinkedIn: https://www.linkedin.com/in/robertbreen
π Nodes Used
Google BigQuery, AI Agent, OpenAI Chat Model, Simple Memory, Structured Output Parser, Chat Trigger
π₯ Import
Download workflow.json and import into n8n:
Workflow menu β Import from File