Translate Questions About E-mails into SQL Queries and Execute Them

This workflow utilizes natural language processing technology to convert email queries posed by users through chat into SQL statements, which are then executed directly to return results. It simplifies the writing of complex SQL statements, lowering the technical barrier, and is suitable for scenarios such as enterprise email data analysis and quick identification of email records for customer support. Through multi-turn conversations and manual triggers, users can efficiently and accurately retrieve email data, enhancing work efficiency, making it an effective tool for intelligent email data retrieval.

Workflow Diagram
Translate Questions About E-mails into SQL Queries and Execute Them Workflow diagram

Workflow Name

Translate Questions About E-mails into SQL Queries and Execute Them

Key Features and Highlights

This workflow leverages natural language processing technology to intelligently convert user queries about emails—posed in a conversational format—into precise SQL queries that align with the underlying database schema. It then executes these queries directly and returns the results. The workflow integrates multiple capabilities including automatic database introspection, schema parsing, intelligent semantic understanding, and SQL generation. It supports multi-turn conversational triggers as well as manual execution, offering flexibility and high efficiency.

Core Problems Addressed

Users often find it challenging to write complex SQL queries for email databases, especially when dealing with varying table structures and data types. This workflow bridges the gap between natural language queries and database operations, eliminating the need for users to understand SQL syntax. It lowers the technical barrier and enables fast, accurate retrieval of required information from email data.

Application Scenarios

  • Internal enterprise email data analysis and retrieval
  • Customer support teams quickly locating email records
  • Personal email management and historical queries
  • Product managers or data analysts extracting insights from email content
  • Educational or training tools demonstrating natural language to SQL conversion

Main Process Steps

  1. Trigger the workflow manually or via chat interaction.
  2. Query the database to list all tables and their fields, constructing the database schema.
  3. Convert the schema information into a locally stored JSON file for subsequent use.
  4. Receive the user’s natural language query input.
  5. Use the built-in AI Agent, combined with the database schema, to strictly generate SQL statements compliant with PostgreSQL syntax.
  6. Automatically check and complete SQL syntax formatting (e.g., appending a semicolon).
  7. Execute the generated SQL query and retrieve the results.
  8. Format the query results and merge them with the AI’s response before returning to the user.

Involved Systems or Services

  • PostgreSQL Database: Stores and queries email data and metadata
  • n8n Workflow Automation Platform: Manages node orchestration and process control
  • LangChain AI Agent (Ollama Chat Model phi4-mini:latest): Handles natural language understanding and SQL generation
  • Local File System: Caches database schema files to improve efficiency
  • Webhook and Chat Triggers: Support real-time interaction and automatic triggering

Target Users and Value Proposition

  • Business users unfamiliar with SQL who need to query email databases
  • Data analysts and product managers seeking to improve query efficiency
  • IT operations and developers simplifying database query scripting
  • Enterprise customer support teams rapidly locating customer email histories
  • Any users wishing to operate databases directly via natural language, significantly reducing learning curves and enhancing productivity

By combining AI intelligence with automation, this workflow delivers a convenient experience of “asking questions in chat and obtaining SQL query results,” making it a practical tool in the domain of intelligent email data retrieval.

Translate Questions About E-mails into SQL Queries and Execute Them