BBampaa

Lessons › SQL › Go further in SQL: Business Analytics Patterns

SQL lesson

Funnel conversion analysis

Funnel conversion analysis tracks what percentage of people make it through each step of a multi-stage process, so you can see exactly where the biggest drop-offs occur.

Try this lesson free →
About 5–10 minutes · no sign-up needed

Example

Funnel conversion

SELECT status,
       COUNT(*) AS stage_count,
       ROUND(100.0 * COUNT(*) / SUM(COUNT(*)) OVER (), 1) AS pct_of_total
FROM orders
GROUP BY status
ORDER BY stage_count DESC

Result:

statusstage_countpct_of_total
cart342.9
payment228.6
address114.3
confirmation114.3

Step by step

  1. Run the query without COUNT and ROUND so you can see what the unfiltered values look like.
    SELECT * FROM orders;
  2. COUNT tells you how many rows match - perfect for sizing a table. ROUND rounds the value to the nearest integer (or to N decimal places with a second argument).
    SELECT status, COUNT(*), ROUND(100.0 * COUNT(*) / SUM(COUNT(*)) OVER (), 1) FROM orders GROUP BY status ORDER BY COUNT(*) DESC;
  3. AS gives the COUNT and ROUND results friendly column names so the output reads cleanly.
    SELECT status, COUNT(*) AS stage_count, ROUND(100.0 * COUNT(*) / SUM(COUNT(*)) OVER (), 1) AS pct_of_total FROM orders GROUP BY status ORDER BY stage_count DESC;

Practice

Try each one in your head first, then open the answer.

1. Using events, count the users who reached each stage of the checkout funnel, in funnel order. Show step, stage and users.
SELECT step, stage, COUNT(DISTINCT user_id) AS users FROM events GROUP BY step, stage ORDER BY step
2. Using events, what percent of visitors went on to make a purchase? Show it as conversion_pct, rounded to 1 decimal place.
SELECT ROUND(100.0 * COUNT(DISTINCT CASE WHEN stage = 'purchase' THEN user_id END) / COUNT(DISTINCT CASE WHEN stage = 'visit' THEN user_id END), 1) AS conversion_pct FROM events
3. Using events, show each stage's users and what percent of all visitors (step 1) reached it, rounded to 1 decimal, in funnel order. Name the columns step, stage, users and pct_of_visitors.
SELECT step, stage, COUNT(DISTINCT user_id) AS users, ROUND(100.0 * COUNT(DISTINCT user_id) / MAX(COUNT(DISTINCT user_id)) OVER (), 1) AS pct_of_visitors FROM events GROUP BY step, stage ORDER BY step
4. Using events, show each stage's users and what percent of the previous stage's users got there, rounded to 1 decimal (empty for step 1). Name the columns step, stage, users and pct_of_previous.
WITH s AS (SELECT step, stage, COUNT(DISTINCT user_id) AS users FROM events GROUP BY step, stage) SELECT step, stage, users, ROUND(100.0 * users / LAG(users) OVER (ORDER BY step), 1) AS pct_of_previous FROM s ORDER BY step
5. Using events, find the stage where the funnel loses the most people: show the step, stage and users_lost (users at the step before minus users at this step) for the biggest drop.
WITH s AS (SELECT step, stage, COUNT(DISTINCT user_id) AS users FROM events GROUP BY step, stage), d AS (SELECT step, stage, LAG(users) OVER (ORDER BY step) - users AS users_lost FROM s) SELECT step, stage, users_lost FROM d WHERE users_lost IS NOT NULL ORDER BY users_lost DESC LIMIT 1

Learn it by doing. This lesson is a short hands-on level on Bampaa: see it work, then build and write it yourself, with instant feedback.

Try this lesson free →
About 5–10 minutes · no sign-up needed