Generate SQL queries from schema only - AI-powered

For the platform n8n, this workflow automates the generation of SQL queries from a database schema using AI. It simplifies database interactions by allowing users to chat with an AI agent that understands the schema, improving efficiency and reducing the need for manual SQL writing. Users can quickly retrieve data without needing to know SQL syntax, streamlining data access and enhancing productivity.

7/8/2025
29 nodes
Complex
manualcomplexlangchainnoopmysqlconverttofilereadwritefileextractfromfilesticky noteadvanceddatabasedatafilesstoragelogicconditional
Categories:
Complex WorkflowManual TriggeredData Processing & Analysis
Integrations:
LangChainNoOpMySQLConvertToFileReadWriteFileExtractFromFileSticky Note

Target Audience

Target Audience


- Data Analysts: Individuals analyzing database schemas and querying data efficiently.
- Developers: Those who need to automate SQL query generation and execution without manual intervention.
- Data Scientists: Professionals who require quick access to structured data for analysis.
- Business Intelligence Professionals: Users who want to extract insights from databases using natural language queries.
- Educators and Students: Anyone learning about database interactions and automation workflows.

Problem Solved

Problem Solved


This workflow addresses the challenge of generating SQL queries from a database schema based on user input. It automates the process of:
- Extracting Database Schema: Users can retrieve the structure of the database without needing to know SQL.
- Generating Queries: It allows users to formulate queries using natural language, making it accessible to those without SQL expertise.
- Executing Queries: Users can run their generated queries and receive formatted results, streamlining data retrieval and analysis.

Workflow Steps

Workflow Steps


1. Trigger: The workflow is manually initiated via a trigger node.
2. List Tables: It retrieves all tables from the specified MySQL database.
3. Extract Schema: For each table, the schema is extracted and stored for further processing.
4. Convert to JSON: The schema data is converted to a binary JSON format for efficient storage.
5. Save Locally: The binary JSON schema is saved as chinook_mysql.json locally.
6. Chat Interaction: Users can interact with the system through a chat interface where they input their queries.
7. Load Schema: Upon receiving a query, the workflow loads the schema from the local file to ensure quick access.
8. Generate SQL Query: The AI agent generates an SQL query based on the user input and the schema.
9. Check Query: It checks if the generated query is valid and exists.
10. Run SQL Query: If a valid query exists, it executes the query against the database.
11. Format Results: The results from the query are formatted for readability and combined with the chat response.
12. Output: Finally, the combined output of the chat response and the query results is provided to the user.

Customization Guide

Customization Guide


- Change Database Connection: Update the MySQL credentials in the workflow to connect to a different database.
- Modify Queries: Users can adjust the SQL queries generated by the AI agent by modifying the input prompts or the agent's behavior.
- Enhance Schema Extraction: Customize the schema extraction process to include additional metadata or constraints as needed.
- Adjust Output Format: Change how the query results are formatted before they are displayed to the user, ensuring clarity or adding additional context.
- Add Additional Nodes: Integrate other n8n nodes for additional processing, such as sending results via email or storing them in a different format.