
Multi-Agent SQL Audience Selection: Intelligent Marketing Automation
Sophisticated n8n automation using Multi-Agent AI (GPT-4o, Gemini) to orchestrate complex SQL audience targeting and content generation.
Project Overview
The Challenge
Marketing teams needed to select highly specific audiences for 50+ events/year but relied on static, outdated lists and manual SQL queries from IT.
Engagement data (webinars, downloads) was siloed from profile data, making behavioral targeting impossible.
Manual content creation for each event (LinkedIn posts, emails) was a bottleneck, reducing the number of events that could be promoted.
No feedback loop existed to refine audience selection based on past performance or 'lookalike' criteria.
Architected a complex Multi-Agent automation system using n8n to revolutionize event audience selection. The system allows non-technical users to query a SQL database using natural language, orchestrated by a team of specialized AI agents (Orchestrator, Keyword Generator, SQL Validator) to ensure 100% accurate and safe query execution.
Implemented a sophisticated 1,000-point scoring algorithm combining behavioral signals (event attendance, content interaction) and profile fit. This logic was translated into dynamic SQL Common Table Expressions (CTEs) generated by AI, enabling granular targeting previously impossible with static lists.
Built a seamless user experience using Microsoft Teams Adaptive Cards for approval workflows and an interactive web form for input. The system includes a robust SQL proxy for security, automated content generation (LinkedIn posts + banners) using Gemini and OpenAI, and email orchestration via Mailjet.
Technical Architecture
n8n Orchestrator: Central hub managing the state and flow between agents, database, and user interfaces.
AI Agent Swarm: A network of specialized LLM agents (using LangChain nodes in n8n) handling specific tasks: understanding intent, generating keywords, writing SQL, and validating safety.
SQL Proxy Layer: A custom secure middleware ensuring AI-generated SQL is read-only and performance-optimized before hitting the production database.
Human-in-the-Loop: Critical approval steps integrated via Microsoft Teams Adaptive Cards, allowing non-technical users to validate audience segments before campaign launch.
Key Challenges & Solutions
Natural Language to Reliable SQL
Solved the hallucination problem by implementing a 3-step verification process: Keyword Expansion → SQL Generation → Syntax Validation Agent. The system safely handles complex logic like 'people who attended X but not Y'.
Secure AI-DB Interaction
Built a custom SQL proxy middleware to sanitize inputs and enforce read-only access, preventing any risk of SQL injection or data corruption from AI-generated queries.
Handling Abstract Scoring Logic
Translated a subjective 1,000-point business scoring model into concrete, performant SQL Common Table Expressions (CTEs) that calculate scores in real-time across millions of rows.
Complex Multi-Step Approvals
Engineered a robust state management system within n8n to handle long-running workflows that pause for human approval on Teams (waiting for webhook callbacks) without timing out.
Impact & Results
Automated audience selection reducing manual data work from 3 days to 3 minutes per event
Enabled behavioral targeting increasing event registration rates by 40% (estimated)
Zero SQL knowledge required for marketing team to access complex database insights
Fully automated content supply chain (Image + Copy + Audience) ready for 1-click launch
Key Features
- Multi-Agent AI Orchestration (GPT-4o, GPT-5.2, Gemini)
- Natural Language to SQL conversion with validation
- 1,000-point audience scoring algorithm
- Interactive Teams Adaptive Cards for approval
- Automated LinkedIn post & banner generation
- Secure SQL Proxy for database interaction
- Dynamic audience expansion logic (threshold reduction)
- Keyword diversification agent (150+ variants)
- Automated email marketing orchestration (Mailjet)
- Complex n8n workflow with subgraph architecture
Technologies Used
Project Gallery



Project Details
Client
French client - Via Upwork
Timeline
December 2024
Role
Automation Architect & Backend Developer
© 2026 Firas Jday. All rights reserved.

