Problem
Create intelligent marketing campaigns by analyzing customer behavior, segmenting audiences, predicting churn, and generating personalized content using AI-powered insights.
What You'll Learn
- Segment customers using AI-powered clustering
- Analyze campaign performance and customer sentiment
- Predict customer lifetime value and churn risk
- Generate personalized marketing content automatically
Setup: Create Marketing Database
CREATE TABLE customers (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
email TEXT,
age INTEGER,
gender TEXT,
location TEXT,
signup_date DATE,
total_spent DECIMAL(10,2),
last_purchase_date DATE,
purchase_frequency INTEGER,
preferred_channel TEXT,
customer_profile VECTOR(384)
);
CREATE TABLE campaigns (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
type TEXT,
channel TEXT,
start_date DATE,
end_date DATE,
budget DECIMAL(10,2),
target_audience TEXT,
content TEXT,
campaign_embedding VECTOR(384)
);
CREATE TABLE campaign_interactions (
id INTEGER PRIMARY KEY,
customer_id INTEGER REFERENCES customers(id),
campaign_id INTEGER REFERENCES campaigns(id),
interaction_type TEXT,
interaction_date TIMESTAMP,
conversion BOOLEAN DEFAULT FALSE,
revenue DECIMAL(10,2) DEFAULT 0
);
CREATE TABLE customer_feedback (
id INTEGER PRIMARY KEY,
customer_id INTEGER REFERENCES customers(id),
feedback_type TEXT,
rating INTEGER CHECK (rating >= 1 AND rating <= 5),
comment TEXT,
feedback_date DATE,
sentiment_score REAL
);
CREATE TABLE product_purchases (
id INTEGER PRIMARY KEY,
customer_id INTEGER REFERENCES customers(id),
product_name TEXT,
category TEXT,
price DECIMAL(10,2),
purchase_date DATE,
quantity INTEGER
);
Seed Data: Customer Base
INSERT INTO customers (id, name, email, age, gender, location, signup_date, total_spent, last_purchase_date, purchase_frequency, preferred_channel) VALUES
(1, 'Emma Wilson', 'emma.w@email.com', 28, 'Female', 'New York, NY', '2024-01-15', 1250.50, '2025-01-10', 8, 'Email'),
(2, 'Michael Chen', 'michael.c@email.com', 35, 'Male', 'San Francisco, CA', '2023-11-20', 2100.75, '2025-01-08', 12, 'Social Media'),
(3, 'Sarah Johnson', 'sarah.j@email.com', 42, 'Female', 'Chicago, IL', '2024-03-10', 890.25, '2024-12-15', 4, 'Email'),
(4, 'David Rodriguez', 'david.r@email.com', 31, 'Male', 'Austin, TX', '2024-06-05', 1650.00, '2025-01-12', 10, 'Mobile App'),
(5, 'Lisa Park', 'lisa.p@email.com', 26, 'Female', 'Seattle, WA', '2024-08-20', 750.80, '2025-01-14', 6, 'Social Media'),
(6, 'James Martinez', 'james.m@email.com', 38, 'Male', 'Miami, FL', '2023-09-12', 3200.40, '2025-01-11', 15, 'Email'),
(7, 'Anna Kim', 'anna.k@email.com', 29, 'Female', 'Los Angeles, CA', '2024-02-28', 1100.60, '2024-11-20', 3, 'Mobile App'),
(8, 'Robert Taylor', 'robert.t@email.com', 45, 'Male', 'Boston, MA', '2023-12-03', 2800.90, '2025-01-09', 14, 'Email');
INSERT INTO campaigns (id, name, type, channel, start_date, end_date, budget, target_audience, content) VALUES
(1, 'Spring Fashion Sale', 'Promotional', 'Email', '2025-01-01', '2025-01-31', 5000.00, 'Women 25-40', 'Discover our new spring collection with 30% off on all items!'),
(2, 'Tech Gadgets Launch', 'Product Launch', 'Social Media', '2025-01-10', '2025-01-25', 8000.00, 'Men 25-45', 'Revolutionary tech gadgets that will change your daily routine. Pre-order now!'),
(3, 'Loyalty Rewards Program', 'Retention', 'Mobile App', '2025-01-05', '2025-01-20', 3000.00, 'High-value customers', 'Exclusive rewards for our most valued customers. Unlock special benefits today!'),
(4, 'Winter Clearance', 'Clearance', 'Email', '2024-12-01', '2024-12-31', 2000.00, 'Price-sensitive shoppers', 'Last chance! Up to 70% off on winter items. Limited time only!'),
(5, 'Valentine\'s Day Special', 'Seasonal', 'Social Media', '2025-02-01', '2025-02-14', 4500.00, 'Couples 25-50', 'Show your love with our Valentine\'s Day special collection. Perfect gifts await!');
INSERT INTO product_purchases (id, customer_id, product_name, category, price, purchase_date, quantity) VALUES
(1, 1, 'Wireless Headphones', 'Electronics', 199.99, '2025-01-10', 1),
(2, 1, 'Summer Dress', 'Fashion', 89.50, '2024-12-20', 2),
(3, 2, 'Smart Watch', 'Electronics', 299.99, '2025-01-08', 1),
(4, 2, 'Laptop Bag', 'Accessories', 79.99, '2024-12-15', 1),
(5, 3, 'Coffee Maker', 'Home & Kitchen', 149.99, '2024-12-15', 1),
(6, 4, 'Running Shoes', 'Sports', 129.99, '2025-01-12', 1),
(7, 4, 'Fitness Tracker', 'Electronics', 199.99, '2025-01-05', 1),
(8, 5, 'Skincare Set', 'Beauty', 89.99, '2025-01-14', 1),
(9, 6, 'Premium Suit', 'Fashion', 599.99, '2025-01-11', 1),
(10, 6, 'Leather Shoes', 'Fashion', 249.99, '2024-12-20', 1);
INSERT INTO campaign_interactions (id, customer_id, campaign_id, interaction_type, interaction_date, conversion, revenue) VALUES
(1, 1, 1, 'OPEN', '2025-01-15 09:30:00', TRUE, 89.50),
(2, 2, 2, 'CLICK', '2025-01-12 14:20:00', FALSE, 0),
(3, 3, 4, 'OPEN', '2024-12-10 10:15:00', TRUE, 149.99),
(4, 4, 3, 'CLICK', '2025-01-07 16:45:00', TRUE, 199.99),
(5, 5, 1, 'OPEN', '2025-01-16 11:20:00', FALSE, 0),
(6, 6, 2, 'CLICK', '2025-01-13 19:30:00', TRUE, 299.99),
(7, 7, 4, 'OPEN', '2024-12-12 08:45:00', FALSE, 0),
(8, 8, 3, 'CLICK', '2025-01-08 12:10:00', TRUE, 599.99);
INSERT INTO customer_feedback (id, customer_id, feedback_type, rating, comment, feedback_date) VALUES
(1, 1, 'Product Review', 5, 'Amazing headphones! Great sound quality and comfortable fit.', '2025-01-12'),
(2, 2, 'Service Review', 4, 'Fast delivery but packaging could be better.', '2025-01-10'),
(3, 3, 'Product Review', 3, 'Coffee maker works fine but instructions were unclear.', '2024-12-18'),
(4, 4, 'Service Review', 5, 'Excellent customer service and quick response to my questions.', '2025-01-14'),
(5, 6, 'Product Review', 5, 'Perfect suit! Great quality and perfect fit.', '2025-01-13');
Generate Customer and Campaign Embeddings
UPDATE customers
SET customer_profile = EMBED(
name || ' ' || age || ' ' || gender || ' ' || location || ' ' ||
preferred_channel || ' ' || total_spent || ' ' || purchase_frequency
);
UPDATE campaigns
SET campaign_embedding = EMBED(name || ' ' || type || ' ' || target_audience || ' ' || content);
Analyze Customer Sentiment
UPDATE customer_feedback
SET sentiment_score = SENTIMENT(comment);
Customer Segmentation Analysis
WITH customer_segments AS (
SELECT
c.*,
CASE
WHEN c.total_spent > 2000 AND c.purchase_frequency > 10 THEN 'VIP'
WHEN c.total_spent > 1000 AND c.purchase_frequency > 6 THEN 'Loyal'
WHEN c.total_spent > 500 AND c.purchase_frequency > 3 THEN 'Regular'
WHEN c.last_purchase_date < DATE('2024-12-01') THEN 'At Risk'
ELSE 'New'
END as segment,
CASE
WHEN age < 30 THEN 'Gen Z/Young Millennial'
WHEN age < 40 THEN 'Millennial'
WHEN age < 50 THEN 'Gen X'
ELSE 'Boomer'
END as age_group
FROM customers c
)
SELECT
segment,
age_group,
COUNT(*) as customer_count,
AVG(total_spent) as avg_spending,
AVG(purchase_frequency) as avg_frequency,
GROUP_CONCAT(DISTINCT preferred_channel) as preferred_channels
FROM customer_segments
GROUP BY segment, age_group
ORDER BY avg_spending DESC;