SQL Conversion Rate
Measure the share of leads in each acquisition source that reached conversion.
What & Why
Conversion rate divides converted entities by the complete eligible population at the same grain. Here, a marketing lead is converted when converted_at IS NOT NULL.
Grouping both counts by source keeps each source's converted-lead numerator aligned with its all-leads denominator.
See How It Works
BUSINESS QUESTION
Calculate lead conversion rate for every acquisition source.
| id | campaign_id | created_at | qualified_at | converted_at | lead_score | source | country | |
|---|---|---|---|---|---|---|---|---|
| 301 | 1 | ana@example.com | 2024-01-21 09:10:00+00 | 2024-01-22 11:00:00+00 | 2024-02-02 10:00:00+00 | 86 | google_ads | US |
| 302 | 1 | ben@example.com | 2024-01-24 12:40:00+00 | NULL | NULL | 52 | google_ads | CA |
| 303 | 2 | chloe@example.com | 2024-02-16 08:30:00+00 | 2024-02-18 14:20:00+00 | NULL | 74 | GB | |
| 304 | 3 | dev@example.com | 2024-03-20 17:15:00+00 | 2024-03-21 09:00:00+00 | 2024-04-04 16:00:00+00 | 91 | US |
EXAMPLE QUERY
SELECT
source,
COUNT(*) AS leads,
COUNT(*) FILTER (WHERE converted_at IS NOT NULL) AS converted_leads,
ROUND(COUNT(*) FILTER (WHERE converted_at IS NOT NULL)::numeric / NULLIF(COUNT(*), 0) * 100, 2) AS conversion_rate_pct
FROM marketing.leads
GROUP BY source
ORDER BY conversion_rate_pct DESC NULLS LAST, source;RESULT — lead conversion by source
| source | leads | converted_leads | conversion_rate_pct |
|---|---|---|---|
| 1 | 1 | 100.00 | |
| google_ads | 2 | 1 | 50.00 |
| 1 | 0 | 0.00 |
Converted timestamps are present for lead 301 and lead 304.
This lesson's practice is part of Pro.
Sign up free to try it on a real business scenario