Funnels
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:
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;
| login | view | cart | buy |
|---|---|---|---|
| 24 | 21 | 13 | 9 |
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.
Counting rows without removing duplicates, what do you get for the buy step?
1SELECT action, COUNT(*) AS n2FROM events3WHERE action = 'buy'4GROUP BY action;
buy has 13 rows, but they come from only 9 people: some bought two or three times. A funnel asks "how many people got to this step", so you need COUNT(DISTINCT user_id). Counting rows tells you "how many times this step happened".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:
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;
| view_to_cart | cart_to_buy | view_to_buy |
|---|---|---|
| 61.9 | 69.2 | 42.9 |
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 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;
carted. Dividing by viewed gives "of the people who viewed, how many ended up buying", which is a different number.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:
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;
| step | action | users | pct_of_first | pct_of_prev |
|---|---|---|---|---|
| 1 | login | 24 | 100.0 | NULL |
| 2 | view | 21 | 87.5 | 87.5 |
| 3 | cart | 13 | 54.2 | 61.9 |
| 4 | buy | 9 | 37.5 | 69.2 |
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.
| step | action | users | pct_of_prev |
|---|---|---|---|
| 1 | login | 24 | NULL |
| 2 | view | 21 | 87.5 |
| 3 | cart | 13 | 61.9 |
| 4 | buy | 9 | 69.2 |
Why write a separate steps with the numbers 1, 2, 3 and 4?
ORDER BY step, FIRST_VALUE and LAG can follow the real order. It's the same idea as the days of the week in lesson 2.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.
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.
| channel | viewers | buyers | buy_pct |
|---|---|---|---|
| friend | 2 | 1 | 50.0 |
| search | 6 | 3 | 50.0 |
| ads | 5 | 2 | 40.0 |
| social | 8 | 3 | 37.5 |
1SELECT u.channel2FROM events AS e3JOIN users AS u ON u.id = e.user_id4GROUP BY u.channel;
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):
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;
| visits | viewed | carted | bought |
|---|---|---|---|
| 110 | 64 | 23 | 13 |
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 these lines in order to make a query that runs: how many visits each day added something to the cart.
FROM visitsGROUP BY daySELECT day, SUM(carted) AS cartsLIMIT 3;ORDER BY dayWITH 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)
day | carts 2025-12-01 | 1 2025-12-02 | 1 2025-12-03 | 0
visits, then add up carted for each day. MAX is used because even if someone added to cart several times in a day, that visit only counts once as "added to cart".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 useFIRST_VALUEfor the first step andLAGfor 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 useMAX(CASE …)to mark whether that visit reached a step. Conversion rates by person and by visit are different, so say which one you use.