Kodewick
Start learning →
Funnels9 / 12

Funnels

Lesson 9 of 12 · Part 3 · Analysing users · about 20 minutes

People at each step: COUNT(DISTINCT CASE WHEN action = 'view' THEN user_id END). Conversion rate = this step ÷ the step before. For a funnel with one step per row, list the steps with UNION ALL, then use FIRST_VALUE and LAG for the share of the first step and of the step before. To count by visit, first gather each person's day into one row.

After opening the app, a user has to go through several steps before paying: log in → look at products → add to cart → buy. At every step some people leave, so the numbers get smaller and smaller, and drawn as a picture it looks like a funnel. What the boss most wants to know: at which step do we lose most people?

How many people at each step

In this step you count how many people reach each step. In events, action records what someone did. A person who looked at products 5 times still counts as just 1 person who viewed, so you count different people:

main.sql
1SELECT COUNT(DISTINCT CASE WHEN action = 'login' THEN user_id END) AS login,2    COUNT(DISTINCT CASE WHEN action = 'view' THEN user_id END) AS view,3    COUNT(DISTINCT CASE WHEN action = 'cart' THEN user_id END) AS cart,4    COUNT(DISTINCT CASE WHEN action = 'buy' THEN user_id END) AS buy5FROM events;
Output
loginviewcartbuy
2421139
1 row

24 people logged in, 21 viewed products, 13 added something to the cart and 9 bought. It's the "count by condition" trick from lesson 3, just with DISTINCT inside the COUNT.

Choose oneA wrong guess is fine

Counting rows without removing duplicates, what do you get for the buy step?

1SELECT action, COUNT(*) AS n2FROM events3WHERE action = 'buy'4GROUP BY action;

Conversion rates

In this step you turn the counts into shares. The conversion rate is the number of people at this step ÷ the number at the step before. First work out the counts in WITH, then divide outside:

main.sql
1WITH funnel AS (2    SELECT COUNT(DISTINCT CASE WHEN action = 'view' THEN user_id END) AS viewed,3        COUNT(DISTINCT CASE WHEN action = 'cart' THEN user_id END) AS carted,4        COUNT(DISTINCT CASE WHEN action = 'buy' THEN user_id END) AS bought5    FROM events6)7SELECT ROUND(100 * carted / viewed, 1) AS view_to_cart,8    ROUND(100 * bought / carted, 1) AS cart_to_buy,9    ROUND(100 * bought / viewed, 1) AS view_to_buy10FROM funnel;
Output
view_to_cartcart_to_buyview_to_buy
61.969.242.9
1 row

From viewing to adding to cart is only 61.9%, and from cart to buying is 69.2%. The most people are lost between viewing and adding to cart: maybe the product pages aren't appealing enough. That's the step most worth improving.

Fill inTap one to fill a blank; tap a blank to take it back

Fill in the blank: of the people who added to cart, what percentage went on to buy?

1SELECT ROUND(100 * bought / , 1) AS cart_to_buy2FROM funnel;

A funnel table, one step per row

In this step you build the funnel with one step per row, which is easier to chart and easier to add steps to later. First list the steps and their order with UNION ALL, then join them with events and count people. Finally, use two window functions: FIRST_VALUE takes the count of the first step, and LAG takes the count of the step before:

main.sql
1WITH steps AS (2    SELECT 1 AS step, 'login' AS action3    UNION ALL SELECT 2, 'view'4    UNION ALL SELECT 3, 'cart'5    UNION ALL SELECT 4, 'buy'6),7funnel AS (8    SELECT s.step, s.action, COUNT(DISTINCT e.user_id) AS users9    FROM steps AS s10    JOIN events AS e ON e.action = s.action11    GROUP BY s.step, s.action12)13SELECT step, action, users,14    ROUND(100 * users / FIRST_VALUE(users) OVER (ORDER BY step), 1) AS pct_of_first,15    ROUND(100 * users / LAG(users) OVER (ORDER BY step), 1) AS pct_of_prev16FROM funnel17ORDER BY step;
Output
stepactionuserspct_of_firstpct_of_prev
1login24100.0NULL
2view2187.587.5
3cart1354.261.9
4buy937.569.2
4 rows

pct_of_first is "how many are left": of the people who logged in, 37.5% ended up buying. pct_of_prev is the conversion rate of each step. The first step has no step before it, so it's NULL.

funnel
stepactionuserspct_of_prev
1login24NULL
2view2187.5
3cart1361.9
4buy969.2
One step per row: the numbers keep shrinking, so look for the step with the biggest drop
Choose one

Why write a separate steps with the numbers 1, 2, 3 and 4?

Compare by channel

In this step you compare users who came from different places: from search, from ads, from a friend… Who's more likely to buy? Join events with users, group by channel, and you get a small funnel for each group.

Exercise 1

Which channel's people buy the most

For each channel, work out how many people viewed, how many bought, and the buy rate (people who bought ÷ people who viewed × 100, rounded to 1 decimal place). Four columns: channel, viewers, buyers and buy_pct, sorted by buy_pct from highest to lowest, with ties sorted by channel.

It should print
channelviewersbuyersbuy_pct
friend2150.0
search6350.0
ads5240.0
social8337.5
4 rows
exercise1.sql
1SELECT u.channel2FROM events AS e3JOIN users AS u ON u.id = e.user_id4GROUP BY u.channel;
Output
Press Run or Ctrl+Enter

People from social media view the most but have the lowest buy rate. People from search arrive already knowing what they want, and half of them bought. But only 2 people came from a friend, so that 50% doesn't tell you much.

Counting by visit

In this step you switch to a different unit: not "people" but "visits". If someone views today without buying and comes back tomorrow to buy, counted by person they went through the whole funnel, but counted by visit, only one of their two visits ended in a purchase. Many apps look at "each time someone comes, how likely are they to buy".

First gather each person's day into one row, and use MAX(CASE …) to mark whether they viewed, added to cart or bought that day (1 if they did):

main.sql
1WITH visits AS (2    SELECT user_id, day,3        MAX(CASE WHEN action = 'view' THEN 1 ELSE 0 END) AS viewed,4        MAX(CASE WHEN action = 'cart' THEN 1 ELSE 0 END) AS carted,5        MAX(CASE WHEN action = 'buy' THEN 1 ELSE 0 END) AS bought6    FROM events7    GROUP BY user_id, day8)9SELECT COUNT(*) AS visits, SUM(viewed) AS viewed,10    SUM(carted) AS carted, SUM(bought) AS bought11FROM visits;
Output
visitsviewedcartedbought
110642313
1 row

Out of 110 visits, 64 included looking at products, and only 13 ended in a purchase. By person, 42.9% of the people who viewed went on to buy. By visit, only about 20% of the visits with a view ended in a purchase. Both are right, so a report should say clearly which one it uses.

Put in orderDrag, or use the arrows

Put these lines in order to make a query that runs: how many visits each day added something to the cart.

  1. FROM visits
  2. GROUP BY day
  3. SELECT day, SUM(carted) AS carts
  4. LIMIT 3;
  5. ORDER BY day
  6. WITH visits AS (SELECT user_id, day, MAX(CASE WHEN action = 'cart' THEN 1 ELSE 0 END) AS carted FROM events GROUP BY user_id, day)
Should print
day | carts
2025-12-01 | 1
2025-12-02 | 1
2025-12-03 | 0

What you learned

  • People at each step of a funnel: COUNT(DISTINCT CASE WHEN action = '…' THEN user_id END). Counting rows gives you how many times, not how many people.
  • Conversion rate = this step ÷ the step before. People also often look at the share of the first step.
  • A funnel with one step per row: list the steps and their order with UNION ALL, join the data and count people, then use FIRST_VALUE for the first step and LAG for the step before.
  • Group by channel to get one funnel per group. In small groups, the shares don't tell you much.
  • Counting by visit: first GROUP BY user_id, day, and use MAX(CASE …) to mark whether that visit reached a step. Conversion rates by person and by visit are different, so say which one you use.
Practise what this lesson taught#046COUNT(DISTINCT CASE …)Funnel countsStarter#047COUNT(DISTINCT) / COUNT(DISTINCT)Cart rateBasic#048NOT INCarted, not boughtStarterAll 50 SQL problems →