πŸ“– 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:

  1. Captures a plain-language question from a chat widget or internal portal.
  2. Fetches the current table + column schema from your BigQuery dataset (via INFORMATION_SCHEMA).
  3. 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.
  4. Executes the AI-generated SQL in BigQuery and returns the results.
  5. 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

  1. Import the workflow

    • n8n β†’ Workflows β†’ Import from File (or Paste JSON) β†’ Save
  2. Add credentials

    ServiceWhere to create credentialsNode(s) to update
    OpenAI<https://platform.openai.com> β†’ Create API keyOpenAI Chat Model
    Google BigQueryGoogle Cloud Console β†’ IAM & Admin β†’ Service Account JSON keyGoogle BigQuery (schema + query)
  3. 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_schema with YOUR_PROJECT.YOUR_DATASET.
    • Keep the rest of the query the sameβ€”BigQuery’s INFORMATION_SCHEMA always surfaces table_name, column_name, and data_type.
  4. 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.
  5. (Optional)Embed the chat interface

  6. 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
  7. Activate

    • Toggle Active so the chat assistant is available 24/7.

🧩 Customization Ideas


Contact

πŸ”— 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

πŸ“– Importing guide Β· πŸ”‘ Credential setup