Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Funnel Analysis with CASE WHEN + SUM | Analytical SQL Techniques
SQL for Analytical Reports
Section 1. Chapter 6
single

single

bookFunnel Analysis with CASE WHEN + SUM

Swipe to show menu

Funnel analysis is a core technique in analytics for understanding how users or leads progress through a series of defined business stages, such as from initial awareness to final purchase. In SQL, you can build funnel reports by using the CASE WHEN statement to classify records into funnel stages, and then aggregate these classifications to count the number of users or leads at each stage. This approach allows you to visualize where users drop off and where they convert, providing actionable insights for optimizing business processes.

Note
Definition

A funnel stage represents a key step in a user's journey, such as "Lead," "Trial," or "Paid."
A conversion rate is the percentage of users who move from one stage to the next. For example, if 100 users start a trial and 30 become paid customers, the conversion rate from "Trial" to "Paid" is 30%.

123456
SELECT COUNT(*) AS total_leads, SUM(CASE WHEN status = 'Contacted' THEN 1 ELSE 0 END) AS contacted_leads, SUM(CASE WHEN status = 'Converted' THEN 1 ELSE 0 END) AS converted_leads FROM leads WHERE campaign_id = 8;
copy

The marketing team often needs to understand the effectiveness of their campaigns. For example, they may request a funnel report showing the number of leads generated, how many of those leads started a trial, and how many ultimately converted to paid customers for the last campaign. This type of report helps them identify which steps in the funnel are performing well and which need improvement.

123456789
SELECT COUNT(*) AS total_leads, SUM(CASE WHEN status = 'Contacted' THEN 1 ELSE 0 END) AS trials_started, SUM(CASE WHEN l.status = 'Converted' AND c.conversion_id IS NOT NULL THEN 1 ELSE 0 END) AS paid_conversions FROM leads l LEFT JOIN conversions c ON l.lead_id = c.lead_id WHERE campaign_id = ( SELECT MAX(campaign_id) FROM campaigns );
copy

Now, you will apply these concepts to measure drop-off rates between each stage in the sales funnel. Understanding drop-off at each step is crucial for optimizing the funnel and improving conversion rates.

Task

Swipe to start coding

Write a SQL query to calculate the drop-off rate between each stage of the sales funnel for the most recent campaign.

  • Count the total number of leads for the most recent campaign.
  • Count the number of leads who reached the "Contacted" stage.
  • Count the number of leads who reached the "Converted" stage.
  • Calculate the drop-off percentage from "Lead" to "Contacted" as the percentage of leads who did not reach "Contacted."
  • Calculate the drop-off percentage from "Contacted" to "Converted" as the percentage of contacted leads who did not convert.

Solution

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 1. Chapter 6
single

single

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

some-alt