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 →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| status | stage_count | pct_of_total |
|---|---|---|
| cart | 3 | 42.9 |
| payment | 2 | 28.6 |
| address | 1 | 14.3 |
| confirmation | 1 | 14.3 |
Step by step
- Run the query without COUNT and ROUND so you can see what the unfiltered values look like.
SELECT * FROM orders; - 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; - 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
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 step2. 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 events3. 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 step4. 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 step5. 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 1Learn 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 →