SQL para CRM & Marketing Operations

A library of documented SQL queries for marketers, covering customer segmentation, engagement scoring, campaign reporting, and data hygiene. Written for MySQL with BigQuery equivalency comments throughout. Designed for marketing analysts, CRM managers, and marketing operations professionals who need production-ready queries they can adapt to their own stack.
Schema
All queries reference a generic CRM/marketing schema. Column names follow common conventions used by platforms like HubSpot, RD Station, and Salesforce.
-- contacts
-- One row per contact/lead in the CRM
contact_id VARCHAR(64) PRIMARY KEY
email VARCHAR(255)
first_name VARCHAR(100)
last_name VARCHAR(100)
company VARCHAR(255)
job_title VARCHAR(255)
country CHAR(2)
state CHAR(2)
city VARCHAR(100)
lead_source VARCHAR(100) -- e.g. 'organic', 'paid', 'referral'
lifecycle_stage VARCHAR(50) -- e.g. 'lead', 'mql', 'sql', 'customer'
created_at DATETIME
updated_at DATETIME
-- deals
-- One row per deal/opportunity in the pipeline
deal_id VARCHAR(64) PRIMARY KEY
contact_id VARCHAR(64) FK → contacts.contact_id
deal_name VARCHAR(255)
stage VARCHAR(100) -- e.g. 'prospecting', 'qualified', 'closed_won'
amount DECIMAL(12,2)
currency CHAR(3)
close_date DATE
created_at DATETIME
owner_id VARCHAR(64)
-- campaigns
-- One row per marketing campaign
campaign_id VARCHAR(64) PRIMARY KEY
campaign_name VARCHAR(255)
channel VARCHAR(100) -- e.g. 'email', 'paid_search', 'social', 'organic'
status VARCHAR(50) -- e.g. 'draft', 'active', 'paused', 'completed'
budget DECIMAL(12,2)
start_date DATE
end_date DATE
created_at DATETIME
-- campaign_contacts
-- Junction: which contacts were targeted by which campaign
campaign_id VARCHAR(64) FK → campaigns.campaign_id
contact_id VARCHAR(64) FK → contacts.contact_id
assigned_at DATETIME
PRIMARY KEY (campaign_id, contact_id)
-- events
-- Behavioral events: email opens, clicks, page views, form submissions, purchases
event_id VARCHAR(64) PRIMARY KEY
contact_id VARCHAR(64) FK → contacts.contact_id
campaign_id VARCHAR(64) FK → campaigns.campaign_id -- nullable
event_type VARCHAR(100) -- e.g. 'email_open', 'email_click', 'page_view',
-- 'form_submit', 'purchase', 'unsubscribe'
event_value DECIMAL(12,2) -- revenue for purchases, null for other events
channel VARCHAR(100)
occurred_at DATETIME
-- orders
-- Transactional purchases linked to contacts
order_id VARCHAR(64) PRIMARY KEY
contact_id VARCHAR(64) FK → contacts.contact_id
campaign_id VARCHAR(64) FK → campaigns.campaign_id -- attribution
total_amount DECIMAL(12,2)
currency CHAR(3)
status VARCHAR(50) -- e.g. 'completed', 'refunded', 'cancelled'
ordered_at DATETIME
Query Index
Segmentation — 8 queries
Engagement — 6 queries
Campaigns — 8 queries
Hygiene — 8 queries
Usage
These queries are written for MySQL 8.0+. Each file includes a -- BigQuery: comment block where syntax differs, covering the most common incompatibilities:
| MySQL |
BigQuery |
DATEDIFF(end, start) |
DATE_DIFF(end, start, DAY) |
GROUP_CONCAT(col) |
STRING_AGG(col, ',') |
IFNULL(col, val) |
IFNULL(col, val) (identical) |
NOW() |
CURRENT_DATETIME() |
DATE_FORMAT(col, '%Y-%m') |
FORMAT_DATE('%Y-%m', col) |
All queries are parameterized with clearly labeled constants at the top of each file so thresholds and lookback windows are easy to adjust without editing the core logic.
Adapting to Your Schema
Each query includes a -- Schema note: comment at the top mapping the generic column names to common alternatives found in HubSpot, Salesforce, RD Station, and Klaviyo. Swap the table and column names to match your environment — the logic stays the same.
Tech Stack
CRM & Platforms: HubSpot · RD Station · Salesforce · Klaviyo
Languages: SQL
Tools: Git · GitHub · MySQL · BigQuery
License
Distributed under the MIT License. See LICENSE for details.
Developed by Danilo Campos Espeleta
评论