Translate questions about e-mails into SQL queries and run them

For platform n8n, this workflow translates natural language questions about emails into SQL queries, executes them, and retrieves results. It automates the process of querying email metadata, ensuring accurate data retrieval while saving time and reducing manual effort. Users can easily interact with their email database, generating insights without needing SQL expertise.

7/8/2025
26 nodes
Complex
manualcomplexconverttofilereadwritefileextractfromfilelangchainsticky notepostgresqlexecuteworkflowtriggeradvancedfilesstoragelogicconditionaldatabasedata
Categories:
Complex WorkflowManual TriggeredData Processing & AnalysisBusiness Process Automation
Integrations:
ConvertToFileReadWriteFileExtractFromFileLangChainSticky NotePostgreSQLExecuteWorkflowTrigger

Target Audience

This workflow is designed for:
- Data Analysts: Who need to generate SQL queries from natural language requests related to email metadata.
- Database Administrators: Who want to automate the process of querying email databases and retrieving relevant information efficiently.
- Developers: Looking to integrate natural language processing with SQL databases for improved user interaction and data retrieval.
- Business Intelligence Professionals: Who require quick access to email data for reporting and analysis without needing extensive SQL knowledge.

Problem Solved

This workflow addresses the challenge of translating natural language queries about emails into SQL queries that can be executed against a PostgreSQL database. It simplifies the process for users who may not be familiar with SQL syntax, allowing them to retrieve relevant email data quickly and accurately based on their requests.

Workflow Steps

  • Trigger the Workflow: The workflow can be manually initiated or triggered via a chat interface.
    2. Load Database Schema: It checks for existing schema files or retrieves the schema from the database, ensuring the latest structure is used.
    3. Extract User Input: The workflow captures the user's natural language query and combines it with the schema data.
    4. AI Query Generation: An AI agent generates an appropriate SQL query based on the provided input while adhering to strict schema rules.
    5. Query Execution: The generated SQL query is executed against the PostgreSQL database, and the results are formatted for output.
    6. Results Presentation: The results are combined with the original chat input for seamless user feedback, ensuring clarity and relevance.
  • Customization Guide

    To customize this workflow:
    - Modify the AI Agent Prompt: Adjust the prompt in the AI Agent to refine how SQL queries are generated based on specific user needs or additional rules.
    - Change Database Connection Settings: Update the PostgreSQL connection parameters to point to different databases or schemas as required.
    - Enhance Data Extraction Logic: Add or modify nodes that handle how data is extracted from the database or how results are formatted for display.
    - Integrate Additional Data Sources: If necessary, integrate other data sources or APIs to enrich the queries or provide more context to the user inputs.