Natural Language to SQL Converter for Google Sheets with GPT-4o

Go to Workflow
2 views
Built by Robert Breen Robert Breen
Created on June 05, 2026

Description

This n8n workflow template creates an intelligent data analysis system that converts natural language questions into Google Sheets SQL queries using OpenAI's GPT-4o model. The system generates proper Google Sheets query URLs and executes them via HTTP requests for efficient data retrieval.

What This Workflow Does

Natural Language to SQL**: Converts user questions into Google Sheets SQL syntax
Direct HTTP Queries**: Bypasses API limits by using Google Sheets' built-in query functionality
Column Letter Mapping**: Automatically maps column names to their corresponding letters (A, B, C, etc.)
Structured Query Generation**: Outputs properly formatted Google Sheets query URLs
Real-time Data Access**: Retrieves live data directly from Google Sheets
Memory Management**: Maintains conversation context for follow-up questions

Tools Used

OpenAI Chat Model (GPT-4o)** - SQL query generation and natural language understanding
OpenAI Chat Model (GPT-4.1 Mini)** - Result formatting and table output
Google Sheets Tool** - Column metadata extraction and schema understanding
HTTP Request Node** - Direct data retrieval via Google Sheets query API
Structured Output Parser** - Formats AI responses into executable queries
Memory Buffer Window** - Conversation history management
Chat Trigger** - Webhook-based conversation interface

Step-by-Step Setup Instructions

1. Prerequisites

Before starting, ensure you have:
An n8n instance (cloud or self-hosted)
An OpenAI account with API access and billing setup
A Google account with access to Google Sheets
The target Google Sheet must be publicly accessible or shareable via link

2. Import the Workflow

Copy the workflow JSON provided
In your n8n instance, go to Workflows → Import from JSON
Paste the JSON and click Import
Save with a descriptive name like "Google Sheets SQL Query Generator"

3. Set Up OpenAI Connections

Get API Key:
Go to OpenAI Platform
Sign in or create an account
Navigate to API Keys section
Click Create new secret key
Copy the generated API key
Important: Add billing information and credits to your OpenAI account

Configure Both OpenAI Nodes:
OpenAI Chat Model1 (GPT-4o):
Click on the node
Click Create New Credential
Select OpenAI API
Paste your API key
Save the credential

OpenAI Chat Model2 (GPT-4.1 Mini):
Apply the same OpenAI API credential
This handles result formatting

4. Set Up Google Sheets Connection

Create OAuth2 Credentials:
Go to Google Cloud Console
Create a new project or select existing one
Enable the Google Sheets API
Go to Credentials → Create Credentials → OAuth 2.0 Client IDs
Set application type to Web Application
Add authorized redirect URIs (get this from n8n credentials setup)
Copy the Client ID and Client Secret

Configure in n8n:
Click on the Get Column Info2 node
Click Create New Credential
Select Google Sheets OAuth2 API
Enter your Client ID and Client Secret
Complete the OAuth flow by clicking Connect my account
Authorize the required permissions

5. Prepare Your Google Sheet

Option A: Use the Sample Data Sheet
Access the pre-configured sheet: Sample Marketing Data
Make a copy to your Google Drive
Important**: Set sharing to "Anyone with the link can view"

Critical: Set sharing to "Anyone with the link can view" for HTTP access
Copy the Sheet ID from the URL
Update the Get Column Info2 node with your Sheet ID and column metadata sheet

6. Configure Sheet References

Get Column Info2 Node:
Set Document ID to your Google Sheet ID
Set Sheet Name to your columns metadata sheet (e.g., "Columns")
This provides the AI with column letter mappings

HTTP Request Node:
No configuration needed - it uses dynamic URLs from the AI agent
Ensure your sheet has proper sharing permissions

7. Update System Prompt (If Using Custom Sheet)

If using your own Google Sheet, update the system prompt in the AI Agent3 node:
Replace the URL in the system message with your Google Sheet URL
Update the GID (sheet ID) to match your data sheet
Keep the same query structure format

Contact Information

Robert Ynteractive

For support, customization, or questions about this template:

Email**: [email protected]
LinkedIn**: Robert Breen

Need help implementing this workflow, want to add security features, or require custom modifications? Reach out for professional n8n automation services and AI integration support.

Nodes Used (5)

AI Agent
@n8n/n8n-nodes-langchain.agent
HTTP Request
n8n-nodes-base.httpRequest
OpenAI Chat Model
@n8n/n8n-nodes-langchain.lmChatOpenAi
Simple Memory
@n8n/n8n-nodes-langchain.memoryBufferWindow
Structured Output Parser
@n8n/n8n-nodes-langchain.outputParserStructured