Skip to content
FJFiras JdayTech Lead · Software Engineer
AboutServicesProjectsExperienceBlogGet in touch
Back to projects
  1. Home
  2. Projects
  3. Multi-Agent SQL Audience Selection: Intelligent Marketing Automation
Multi-Agent SQL Audience Selection: Intelligent Marketing Automation
Automation & AI

Multi-Agent SQL Audience Selection: Intelligent Marketing Automation

Multi-agent AI orchestration (GPT-4o, Gemini, Perplexity) generating dynamic SQL audience segments from plain-language briefs — 3 days of marketing analyst work compressed to under 3 minutes.

December 2024 · Automation Architect & Backend Developer

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.

Marketing teams at B2B SaaS companies spend 3–5 days per campaign crafting audience segments — cross-referencing attendee history, firmographics, and engagement scores before a single email sends. This n8n orchestration collapses that cycle to under 3 minutes: a marketer describes the audience in plain language, and a team of specialised AI agents (Orchestrator, Keyword Generator, SQL Validator) translates the intent into validated SQL CTEs against a live PostgreSQL database — no analyst, no ticket, no waiting.

The targeting logic encodes a 1 000-point scoring model combining behavioural signals (event attendance, content interaction, session replays) with profile fit — generating audience cohorts of previously impossible granularity. A Perplexity-backed keyword diversification agent produces 150+ query variants to maximise recall without sacrificing precision.

Approval and output flow through Microsoft Teams Adaptive Cards, keeping stakeholders in their existing tooling. Once approved, the system auto-generates LinkedIn post copy and visual banners (Gemini + OpenAI), then dispatches the campaign via Mailjet — turning a multi-day analyst-and-designer loop into a single supervised run.

Technical architecture

Click diagram to zoom

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

1

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'.

2

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.

3

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.

4

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

3 days of marketing analyst work — audience research, SQL segmentation, copy drafting, banner briefing — compressed to under 3 minutes per campaign

Zero SQL knowledge required: non-technical marketers access complex PostgreSQL audience data through plain-language natural-language queries

1 000-point behavioral scoring model enables audience cohorts previously impossible with static lists or manual segmentation

End-to-end content supply chain automated: audience selection → LinkedIn copy → visual banners → email dispatch, all in a single supervised run

Key features

  • Multi-Agent AI Orchestration (GPT-4o, Gemini, Perplexity)
  • 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

n8nOpenAI API (GPT-4o)Google Gemini APIPerplexity APIMicrosoft Graph API (Teams, OneDrive)PostgreSQLSQLJavaScriptMailjet APIWebhooks

Project gallery

Multi-Agent Orchestration Logic
Multi-Agent Orchestration Logic
Audience Scoring Metrics
Audience Scoring Metrics
Teams Adaptive Card Interface
Teams Adaptive Card Interface

Project details

Client
French client - Via Upwork
Timeline
December 2024
Role
Automation Architect & Backend Developer

More projects

Art A Suivre Marketplace

Art A Suivre Marketplace

E-commerce & Enterprise

LeBonCoin Scraper

LeBonCoin Scraper

Web Scraping & Automation

© 2026 Firas Jday · Tech Lead & Senior Software Engineer · Tunis, Tunisia

Available for CDI & senior freelance — French & international market · jdayfiras.com

0%