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.

idcampaign_idemailcreated_atqualified_atconverted_atlead_scoresourcecountry
3011ana@example.com2024-01-21 09:10:00+002024-01-22 11:00:00+002024-02-02 10:00:00+0086google_adsUS
3021ben@example.com2024-01-24 12:40:00+00NULLNULL52google_adsCA
3032chloe@example.com2024-02-16 08:30:00+002024-02-18 14:20:00+00NULL74emailGB
3043dev@example.com2024-03-20 17:15:00+002024-03-21 09:00:00+002024-04-04 16:00:00+0091linkedinUS
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
sourceleadsconverted_leadsconversion_rate_pct
linkedin11100.00
google_ads2150.00
email100.00

Converted timestamps are present for lead 301 and lead 304.

This lesson's practice is part of Pro.

Advanced business practice

Sign up free to try it on a real business scenario