Most marketing decisions involve questions that your dashboards almost answer. You can see how many leads you got, but not which campaigns produced leads that actually closed. You can see traffic by channel, but not revenue by channel. The step from almost to actually is usually a SQL query.

SQL isn’t hard at the level marketers need. Here are six queries that answer common marketing questions. The sixth is the one most teams are missing.

How do marketers connect to a database to run SQL?

You need access to a database or data warehouse. Common setups for marketing teams:

  • BigQuery (Google Cloud): GA4 can export data here. Free for moderate usage.
  • Redshift or Snowflake: data warehouses, often used by companies with a BI team.
  • CRM reporting databases: some CRMs allow SQL access to their data.
  • Metabase or Looker Studio: friendlier interfaces on top of connected data sources.

The examples below assume a leads table with id, created_at, source, status, deal_value and closed_at.

Query 1: Leads by source this month

SELECT
  source,
  COUNT(*) AS lead_count
FROM leads
WHERE created_at >= DATE_TRUNC('month', CURRENT_DATE)
GROUP BY source
ORDER BY lead_count DESC;

How many leads came from each channel this month. For a fixed range, use WHERE created_at BETWEEN '2026-09-01' AND '2026-09-30'.

Query 2: Close rate by lead source

SELECT
  source,
  COUNT(*) AS total_leads,
  SUM(CASE WHEN status = 'closed_won' THEN 1 ELSE 0 END) AS closed_leads,
  ROUND(
    100.0 * SUM(CASE WHEN status = 'closed_won' THEN 1 ELSE 0 END) / COUNT(*),
    1
  ) AS close_rate_pct
FROM leads
GROUP BY source
ORDER BY close_rate_pct DESC;

One of the most important queries a marketing team can run. A channel with lots of leads and a low close rate is bringing in people who don’t buy. A channel with few leads and a high close rate may deserve more budget. This is also where you see platform numbers drift away from real revenue, because lead quality never shows up in an ad dashboard.

Query 3: Revenue by source

SELECT
  source,
  SUM(deal_value) AS total_revenue,
  AVG(deal_value) AS avg_deal_value,
  COUNT(*) AS deals_closed
FROM leads
WHERE status = 'closed_won'
GROUP BY source
ORDER BY total_revenue DESC;

Actual dollars by channel, not lead counts or close rates. A channel that brings in bigger deals is worth more than its lead count suggests.

Query 4: Average time to close by source

SELECT
  source,
  AVG(
    DATE_PART('day', closed_at - created_at)
  ) AS avg_days_to_close
FROM leads
WHERE status = 'closed_won'
  AND closed_at IS NOT NULL
GROUP BY source
ORDER BY avg_days_to_close ASC;

Sales cycles vary by channel. Referrals often close faster than cold outreach. This matters for forecasting, and it’s the reason you need Query 6.

Query 5: Lead volume trend by month

SELECT
  DATE_TRUNC('month', created_at) AS month,
  source,
  COUNT(*) AS leads
FROM leads
WHERE created_at >= CURRENT_DATE - INTERVAL '6 months'
GROUP BY month, source
ORDER BY month, leads DESC;

A month by month view of leads by source for the last six months. Drop it in a spreadsheet and the trends are easy to see.

Query 6: How do you judge new campaigns fairly?

If you sell anything that takes more than a few days to close, last week’s ad report is lying to you a little.

A lead from last week has had seven days to buy. A lead from two months ago has had sixty. Put them side by side and the new campaign always looks worse, even when it’s doing fine. Owners cut it, and then wonder why sales dip a month later.

The fix is to compare each group of leads after the same number of days:

SELECT
  DATE_TRUNC('week', created_at) AS lead_week,
  source,
  COUNT(*) AS leads,
  SUM(CASE WHEN status = 'closed_won'
            AND closed_at <= created_at + INTERVAL '30 days'
           THEN 1 ELSE 0 END) AS customers_within_30_days,
  ROUND(
    100.0 * SUM(CASE WHEN status = 'closed_won'
                      AND closed_at <= created_at + INTERVAL '30 days'
                     THEN 1 ELSE 0 END) / COUNT(*),
    1
  ) AS close_rate_30d_pct
FROM leads
WHERE created_at < CURRENT_DATE - INTERVAL '30 days'
GROUP BY lead_week, source
ORDER BY lead_week, source;

Every week in the result has had exactly 30 days to turn into customers, so the weeks are finally comparable. Weeks younger than 30 days are left out on purpose. Pick the window from Query 4: if most deals close within 30 days, use 30; if it’s more like 60, use 60.

For the current week, which is too young for this query, look at how fast past weeks filled in. If older weeks typically reached half of their 30 day total within the first seven days, a week with 4 customers after seven days is on pace for about 8. That’s a projection, not a result, and it should be labeled that way in any report.

This is the single most useful habit for anyone reading weekly ad performance. It’s also one of the four reasons cost per customer is so hard to pin down.

How do you get more from SQL as a marketer?

These queries assume a simple data model. Real ones are messier: leads join to accounts, accounts to deals, deals to invoices. The logic doesn’t change. Decide what you’re counting, group by what you care about, filter to the period you want, and make sure you’re comparing like with like.

Once you can pull these answers yourself, the next step is connecting them to paid invoices, not just deals marked won. That’s what makes the numbers hold up, and it’s covered in what RevOps should actually fix first.


d2b2 builds SQL reporting that ties marketing to real revenue, with the timing handled correctly. If you have the data but can’t get straight answers from it, let’s talk.