Beginner Friendly · SQL for Marketing & Customer Analytics

Overview & Introduction to SQL for Marketing

A practical foundation in how marketing data is structured, stored, and queried — built for marketers, analysts, and non-technical professionals.

Modern marketing teams work with data generated across CRM platforms, advertising tools, email systems, and websites. Every customer interaction — a campaign click, a website visit, a purchase — is stored in structured tables. SQL is the tool that connects those tables, enabling marketing professionals to measure campaign performance, identify high-value customers, understand conversion behavior, and generate insights that drive better decisions.
Beginner Friendly
Foundational Concepts
Notes & Reference Guide

Learning Objectives

SQL for Marketing & Customer Analytics is structured as a progressive journey — from foundational data concepts through practical querying to advanced campaign and revenue reporting. Each part builds directly on the knowledge established in the previous one.

Part 1
Marketing Data Foundations

How marketing data is organized across core tables, the distinction between customer-level, campaign-level, and transaction-level data, and how CRM, automation, and ad platforms store customer interactions.

Part 2
Marketing SQL Queries

Hands-on querying — retrieving customer and campaign records, filtering marketing data, joining tables, analyzing website sessions, and aggregating metrics to identify high-value customers and campaign responses.

Part 3
Marketing Analytics & Reporting

Campaign ROI reporting, customer segmentation, cohort analysis, conversion funnel and attribution analysis, customer lifetime value, retention and churn analysis, and building marketing performance dashboards.

Course Goal

By the end of all three parts, learners will have a complete, practical SQL foundation tailored specifically to the real analytical needs of marketing professionals working with customer data, campaign performance, and digital engagement metrics.

Marketing data is structured around customers, the campaigns that reach them, the interactions they have with digital channels, and the purchases they make. Every query in this course draws from one or more of these six core tables.

customers Customer-level Who the customer is
Typical Fields
customer_idnameemailacquisition_channelsignup_datesegment
Used For

Customer segmentation · Acquisition channel analysis · Cohort analysis · Lifetime value calculation. Almost every marketing query begins here — by identifying which customers are being analyzed.

campaigns Campaign-level Each marketing campaign or promotion
Typical Fields
campaign_idcampaign_namechannelstart_dateend_datebudget
Used For

Campaign performance analysis · ROI reporting · Channel comparison · Budget tracking. Joins to engagement and orders tables to measure how campaigns translate into revenue.

orders Transaction-level Every customer purchase
Typical Fields
order_idcustomer_idorder_dateorder_valueproduct_idstatus
Used For

Revenue analysis · Repeat purchase tracking · Customer lifetime value · Product performance. The primary table for any question about what customers bought and how much they spent.

products Product-level Each product or SKU in the catalogue
Typical Fields
product_idproduct_namecategorypricemargin
Used For

Product performance reporting · Category analysis · Price point analysis · Revenue mix. Joined to orders to understand which products drive revenue and repeat purchases.

website_sessions Session-level Each customer visit to the website
Typical Fields
session_idcustomer_idsession_datesourcemediumpages_viewed
Used For

Traffic source analysis · Conversion funnel tracking · Campaign attribution · Bounce and engagement metrics. Connects digital activity to customer records and eventual purchases.

engagement Interaction-level Individual campaign interactions — opens, clicks, conversions
Typical Fields
engagement_idcustomer_idcampaign_idinteraction_typeinteraction_dateconversion_flag
Used For

Email open and click-through rates · Conversion tracking · Campaign response analysis · Attribution modeling. The bridge between campaigns and customer actions.

How the Tables Connect

All six tables connect through shared keys — customer_id links customers to orders, sessions, and engagement records, while campaign_id links campaigns to engagement interactions. Understanding these connections is the foundation of multi-table marketing queries.

customers
 — 1 row per customer
campaigns
 — each marketing activity targeting customers
website_sessions  ·  engagement
 — many interactions per customer per campaign
orders  ·  products
 — transactions resulting from the customer journey
Real-World Connection

These tables mirror the data structure found in platforms such as Salesforce, HubSpot, Google Analytics, Klaviyo, Shopify, and Meta Ads Manager — the systems marketing teams use every day.

Core Principle

Before writing any query, always ask: Am I analyzing customers, campaigns, or transactions? The answer determines which table to start from and how every join should be structured.

Customer-Level
One Row = One Customer

Stores information that does not change per interaction — name, email, acquisition channel, signup date, segment. Use for cohort definitions, segmentation, and lifetime value analysis.

Campaign-Level
One Row = One Campaign

Stores information about each distinct marketing activity — channel, dates, budget, objectives. Use for performance comparisons, ROI reporting, and channel attribution.

Transaction-Level
One Row = One Event

Stores information about each individual interaction or purchase — orders, sessions, engagement events. One customer generates many transaction-level rows over time.

The Full Data Hierarchy

A customer can have many campaign interactions, many website sessions, and many orders. Joining across these levels without accounting for row multiplication is the most common source of inflated counts in marketing analytics.

Level Table Relationship Example Question
Customer customers 1 row per customer "Who are our active customers?"
Campaign campaigns 1 row per campaign "Which campaigns ran last quarter?"
Engagement engagement Many rows per customer per campaign "Which customers clicked on campaign X?"
Session website_sessions Many rows per customer "How many sessions came from paid search?"
Transaction orders Many rows per customer "What is the average order value per segment?"
Golden Rule

Always verify row counts after joining. A customer joined to their orders will produce one row per order — not one row per customer. Summing revenue across that join without accounting for duplicates will inflate totals. Use COUNT(DISTINCT customer_id) when counting customers, not COUNT(*).

Marketing data is built around a set of core fields that appear across nearly every system and query. Understanding what each field means — and how it behaves — is essential before writing any SQL for marketing analysis.

Acquisition Channel — acquisition_channel

Records how a customer first discovered the brand — the source that brought them in. Common values include Organic Search, Paid Search, Email, Social, Referral, and Direct.

  • Identifies which channels drive the most new customers.
  • Supports cost per acquisition (CPA) analysis by channel.
  • Enables cohort analysis by acquisition source — comparing lifetime value across channels.
  • Anchors attribution modeling — understanding which touch points deserve credit for conversions.

Campaign ID — campaign_id

A campaign_id is the key that links engagement and order records back to a specific marketing campaign. Without it, it is impossible to measure whether a campaign actually drove any behavior.

What campaign_id Enables Why It Matters
Linking engagement records to campaigns Measures opens, clicks, and conversions per campaign
Joining campaigns to orders Calculates revenue attributable to each campaign
Comparing campaigns side by side Identifies highest- and lowest-performing activities
Filtering by channel or date range Enables time-based and channel-based performance slicing

Conversion Flag — conversion_flag

A binary field (typically 1 or 0, or TRUE / FALSE) recorded in the engagement table that marks whether a customer interaction resulted in a desired outcome — such as a purchase, a sign-up, or a form completion.

  • Calculates conversion rate — the percentage of interactions that resulted in the target action.
  • Identifies which campaigns, channels, or segments convert at the highest rate.
  • Powers funnel analysis — tracking the drop-off from impression to click to conversion.
  • Feeds into attribution models that assign credit across multiple touch points.

Order Value — order_value

The monetary value of a single transaction, recorded in the orders table. Order value is the foundational field for all revenue analysis in marketing.

  • Calculates average order value (AOV) — a key metric for understanding purchase behavior.
  • Sums into total revenue by campaign, channel, segment, or time period.
  • Feeds into customer lifetime value (CLV) calculations when summed across repeat purchases.
  • Supports ROI reporting — comparing revenue generated against campaign spend.

Why These Fields Are Foundational

Marketing Question Field(s) Required
"Where are our customers coming from?" acquisition_channel
"Which campaign performed best?" campaign_id · conversion_flag · order_value
"What percentage of clicks converted?" conversion_flag
"What is our average order value by segment?" order_value · segment
"What is each customer's lifetime value?" order_value · customer_id

Marketing teams use many different platforms — CRM systems, email automation tools, advertising platforms, and web analytics dashboards. Behind every interface, all of these systems store data in structured tables connected by shared keys. Understanding how each type of platform organizes information helps identify which table to query for each type of question.

CRM Platform
Customer Records

Stores the core customer record — contact details, segment, acquisition source, purchase history, and engagement history. Maps primarily to the customers table. Examples: Salesforce, HubSpot, Zoho.

Marketing Automation
Campaign & Engagement Data

Manages campaign creation, email sends, opens, clicks, and conversions. Maps to the campaigns and engagement tables. Examples: Klaviyo, Mailchimp, Marketo, ActiveCampaign.

Advertising Platforms
Paid Campaign Spend & Performance

Tracks impressions, clicks, conversions, and spend per campaign and ad set. Maps to the campaigns table with spend and performance fields. Examples: Meta Ads, Google Ads, LinkedIn Ads.

Web Analytics
Session & Behaviour Data

Records website sessions, traffic sources, page views, and conversion events. Maps to the website_sessions table. Examples: Google Analytics 4, Adobe Analytics, Mixpanel.

How Data Flows Across the Customer Journey

  1. Customer is acquired — a new customer record is created in the CRM (customers table) with their acquisition channel recorded.
  2. Campaign is launched — the campaign is created in the marketing automation or ad platform (campaigns table) with channel, budget, and dates.
  3. Customer engages — opens, clicks, or interactions are recorded in the engagement table, linked by both customer_id and campaign_id.
  4. Customer visits the website — a session record is created in website_sessions with traffic source and behaviour data.
  5. Customer purchases — a transaction is recorded in orders, linked to the customer and optionally to the campaign that influenced the purchase.
  6. Analysis begins — SQL joins these tables together to measure how campaigns translated into revenue, and which customers are most valuable.
Before Writing Any Query

Always identify which platform domain — CRM, campaign, session, or transaction — the question belongs to. That domain points to the primary table. Every other table in the query is either a join to add context or a filter to narrow the results.

Overview & Introduction — Key Takeaways

Five foundational principles from this introductory section.

Six Tables

Customers, campaigns, orders, products, website sessions, and engagement — knowing each table's level and purpose is the starting point for every marketing query.

Three Levels

Customer-level, campaign-level, and transaction-level data. Understanding which level a table belongs to — and joining correctly — prevents inflated counts and misleading aggregations.

Four Key Fields

Acquisition channel, campaign_id, conversion_flag, and order_value are the backbone of marketing analytics. These fields answer the most fundamental questions about where customers came from, what campaigns drove, and how much revenue was generated.

Four Platforms

CRM, marketing automation, advertising platforms, and web analytics each map to a distinct set of tables. Identifying the correct platform domain before writing a query saves time and prevents errors.

Foundation First

Conceptual clarity prevents analytical errors. A solid grasp of data structure, table levels, and key marketing fields produces accurate, trustworthy results from the very first query.

Enroll Now