Natural Language to Google Sheets SQL Query Generator with GPT-4o - n8n Workflow

Use this powerful n8n workflow to convert natural language questions into Google Sheets Query Language (GQL) using GPT-4o. Automate data extraction and reporting with these advanced n8n templates.

Workflow Preview

Ready to automate?

Download this n8n workflow template and start using it instantly.

Who is this best for?

Data Analysts who frequently query large Google Sheets datasets.
Business users needing quick, conversational access to their data without learning GQL.
Automation Specialists looking for advanced n8n templates involving AI agents and tool use.
Developers building conversational data bots using n8n and OpenAI.

Overview

Writing complex Google Sheets Query Language (GQL) statements can be time-consuming and error-prone. This specialized n8n workflow completely removes that manual step. It provides a conversational interface where users can simply ask data questions (e.g., 'Show me the total sales from California in Q3'), and the system automatically generates the correct, executable GQL query URL. This advanced n8n template utilizes a powerful AI Agent that is given access to your sheet's column metadata via a specific n8n node. This ensures accuracy by making the AI aware of the exact column letters and structure. The subsequent steps in the n8n workflow execute this generated query via an HTTP Request, retrieving the data, and then using a second LLM to format the raw results into a clean, readable table. This solution showcases the power of integrating LLMs deeply within data management using n8n.

How it Works

This powerful automation starts with the 'When chat message received' n8n trigger.


  1. Trigger and Initialization: The n8n workflow is activated when a user sends a message to the associated chat endpoint using the 'When chat message received' n8n trigger node.

  2. AI Agent Analysis: The input message is passed to the main 'AI Agent3' n8n node. This agent is configured with GPT-4o ('OpenAI Chat Model1') and 'Simple Memory2' to maintain conversational context.

  3. Tool Use (Schema Lookup): The agent first uses the 'Get Column Info2' n8n node (a Google Sheets Tool) to retrieve the schema, mapping column letters to field names. The system prompt forces the AI to use only column letters in its query.

  4. Query Generation: Based on the user's question and the column information, the AI Agent generates a complete Google Sheets Query URL (GQL). This output is enforced into a predictable JSON structure by the 'Structured Output Parser1' n8n node.

  5. Data Retrieval: The 'Get Data from Google Sheet' n8n node, which is an HTTP Request node, dynamically uses the URL generated in the previous step to fetch the structured data directly from the Google Sheet.

  6. Formatting and Presentation: Finally, the raw CSV data retrieved is passed to the 'Write into Table Output1' n8n agent. This agent uses a lightweight LLM ('OpenAI Chat Model2', GPT-4.1 Mini) to clean and format the results into a single, cohesive table structure, ready for the user.

Installation Guide

To deploy this cutting-edge n8n workflow, follow these setup steps:


  1. Import: Copy the provided JSON and import it into your n8n instance.

  2. OpenAI Credentials: Create and configure two OpenAI API credentials in the workflow (one for 'OpenAI Chat Model1' and one for 'OpenAI Chat Model2'). Ensure your OpenAI account is provisioned for GPT-4o access.

  3. Google Sheets Credentials (OAuth2): Configure a Google Sheets OAuth2 credential for the 'Get Column Info2' n8n node. You will need to enable the Google Sheets API in the Google Cloud Console and set up a Web Application OAuth Client.

  4. Sheet Configuration: In the 'Get Column Info2' n8n node, set the Document ID and the Sheet Name corresponding to your data source.

  5. System Prompt Update: If you are not using the provided sample sheet, you must update the system prompt within the 'AI Agent3' n8n node. Replace the example Google Sheet URL and GID with your own data sheet's full URL to ensure the generated queries are correct. This step is crucial for the n8n workflow execution.

Node Details

When chat message received (n8n trigger): The starting point of the n8n workflow. Waits for a chat message input to initiate the data query process.
OpenAI Chat Model1 (GPT-4o): Serves as the primary language model for the AI Agent, handling complex natural language translation into structured GQL.
Get Column Info2 (Google Sheets Tool): An essential n8n node tool for the AI Agent. It fetches column names, types, and letter mappings from the target sheet, allowing the AI to construct accurate, column-letter-based GQL.
AI Agent3 (LangChain Agent): The intelligence hub. It orchestrates the process, uses the column tool, and adheres to a strict system prompt to output a fully formed GQL query URL.
Structured Output Parser1: An n8n node used to guarantee the AI's output is clean JSON containing only the query URL, which is vital for the subsequent HTTP request.
Get Data from Google Sheet (HTTP Request): This core n8n node executes the dynamically generated GQL URL to retrieve the requested, aggregated data directly from Google Sheets.


  • Write into Table Output1 (LangChain Agent): Uses a smaller LLM ('OpenAI Chat Model2') to consume the raw data fetched by the previous n8n node and format it clearly for the end user, classifying dimensions and metrics.

Related n8n Workflows

Free

Nodes: 8 Nodes
Updated: December 26 2025
View all
Created by

Professional services consultant with over 10 years of experience solving complex business problems across industries. I specialize in n8n and process automation—designing custom workflows that integrate tools like Google Calendar, Airtable, GPT, and internal systems. Whether you need to automate scheduling, sync data, or streamline operations, I build solutions that save time and drive results.

Featured*