An online shop gets 1,000 visitors. How many sign up? How many add something to the cart? How many finally buy? Each step loses some people, and the shape looks like a funnel, wide at the top and narrow at the bottom. Funnel analysis shows where people drop off, so a team knows which step to fix first. In this lesson you will build a funnel from a table of user events.
In this lesson you will learn
- What an event table looks like
- How to count users at each step with
COUNT(DISTINCT …) - Conversion from the top and from the previous step
- A strict funnel where steps must happen in order
- How long users take to convert
You should know CASE, LAG and percentages.
The Event Table
Apps record one row per action: who did it, what they did and when. The sample events table has 8 users and four steps: visit, signup, add_to_cart and purchase. Some users repeat a step, and user 7 added to the cart before visiting, perhaps from a shared link.
A Simple Funnel
-- How many different users reached each step?
WITH steps(step_no, step) AS (
VALUES (1,'visit'), (2,'signup'), (3,'add_to_cart'), (4,'purchase')
),
counts AS (
SELECT s.step_no, s.step, COUNT(DISTINCT e.user_id) AS users
FROM steps AS s
LEFT JOIN events AS e ON e.step = s.step
GROUP BY s.step_no, s.step
)
SELECT step, users,
ROUND(100.0 * users / FIRST_VALUE(users) OVER (ORDER BY step_no), 1) AS pct_of_visitors,
ROUND(100.0 * users / LAG(users) OVER (ORDER BY step_no), 1) AS pct_of_previous
FROM counts
ORDER BY step_no;
Output
+-------------+-------+-----------------+-----------------+
| step | users | pct_of_visitors | pct_of_previous |
+-------------+-------+-----------------+-----------------+
| visit | 8 | 100.0 | NULL |
| signup | 5 | 62.5 | 62.5 |
| add_to_cart | 4 | 50.0 | 80.0 |
| purchase | 2 | 25.0 | 50.0 |
+-------------+-------+-----------------+-----------------+
(4 rows)
- The
stepslist fixes the order of the steps. Text names would otherwise sort alphabetically. COUNT(DISTINCT e.user_id)counts people, not clicks. User 6 visited twice but counts once.pct_of_visitorsdivides by the first step usingFIRST_VALUE: 25% of visitors bought something.pct_of_previousdivides by the step before usingLAG. Only 62.5% of visitors sign up, and only 50% of carts become purchases. Those are the weakest steps, so look at them first.
A Strict Funnel
The simple funnel counts anyone who ever did a step. A strict funnel only counts a user at a step if they also did every earlier step, in order.
-- Strict funnel: each step must happen after the step before it
WITH firsts AS (
SELECT user_id,
MIN(CASE WHEN step = 'visit' THEN event_time END) AS t_visit,
MIN(CASE WHEN step = 'signup' THEN event_time END) AS t_signup,
MIN(CASE WHEN step = 'add_to_cart' THEN event_time END) AS t_cart,
MIN(CASE WHEN step = 'purchase' THEN event_time END) AS t_buy
FROM events
GROUP BY user_id
)
SELECT COUNT(t_visit) AS visited,
SUM(t_signup > t_visit) AS signed_up,
SUM(t_signup > t_visit AND t_cart > t_signup) AS added_to_cart,
SUM(t_signup > t_visit AND t_cart > t_signup
AND t_buy > t_cart) AS purchased
FROM firsts;
-- Minutes from first visit to purchase, for each buyer
SELECT user_id,
CAST(ROUND((julianday(MAX(CASE WHEN step='purchase' THEN event_time END))
- julianday(MIN(CASE WHEN step='visit' THEN event_time END))) * 24 * 60) AS INTEGER) AS minutes_to_buy
FROM events
GROUP BY user_id
HAVING minutes_to_buy IS NOT NULL;
Output
+---------+-----------+---------------+-----------+
| visited | signed_up | added_to_cart | purchased |
+---------+-----------+---------------+-----------+
| 8 | 5 | 3 | 2 |
+---------+-----------+---------------+-----------+
(1 row)
+---------+----------------+
| user_id | minutes_to_buy |
+---------+----------------+
| 1 | 31 |
| 5 | 1140 |
+---------+----------------+
(2 rows)
- The
firstsCTE finds, for each user, the first time of each step, usingMIN(CASE …). A missing step is NULL. - Each later count adds one more “happened after” check. A comparison with NULL is never true, so users who skipped a step drop out.
- The cart count falls from 4 to 3, because user 7 added to the cart without signing up first. Timestamps written as
YYYY-MM-DD HH:MMtext compare correctly. - The last query measures the time to buy. User 1 took 31 minutes; user 5 came back the next day, 1,140 minutes later.
Making Funnels Useful
- Set a time window. Count a purchase only if it happens within, say, 7 days of the visit.
- Compare groups. Add
GROUP BYon device, city or campaign to see which group drops off more. - Watch sample size. 2 buyers out of 8 is a tiny sample; percentages from small numbers jump around a lot.
Common Mistakes
- Counting events instead of users. Use
COUNT(DISTINCT user_id). - Letting steps sort alphabetically. Give each step a number.
- Ignoring order. A simple funnel can show more people at a later step than an earlier one. Use a strict funnel when order matters.
- Integer division in conversion rates. Multiply by
100.0first.
Practice Exercises
Exercise 1. List the users who signed up but never purchased.
Show answer
SELECT user_id FROM events WHERE step = 'signup'
EXCEPT
SELECT user_id FROM events WHERE step = 'purchase';
Users 2, 3 and 8.
Exercise 2. Using the simple funnel numbers (8 visitors, 2 buyers), what is the overall conversion rate?
Show answer
2 ÷ 8 = 25%.
Frequently Asked Questions
What is funnel analysis in SQL?
It counts how many distinct users reach each step of a process, such as visit, signup and purchase, and calculates the percentage who move from one step to the next. It shows where users drop off.
How do I make sure funnel steps happen in order?
Find each user’s first time for every step with MIN and CASE, then count a user at a step only if its time is later than the previous step’s time. Users who skip or reverse steps are excluded.
Summary
- A funnel counts distinct users at each step of a process.
FIRST_VALUEgives conversion from the top;LAGgives step-to-step conversion.- A strict funnel checks that each step happened after the one before.
- Add time windows and group comparisons to make funnels actionable.
