All problems

Which Acquisition Channel Actually Converts?

hardSQLJoinsGroupBy

Kindled buys traffic from three places and has been judging them by how many sign-ups each one delivers. Paid search wins that contest and has never produced a single sale. The team now wants each channel scored on what fraction of the people it brought in went on to buy.

That fraction is the awkward part, because its denominator includes people who left no trace. A channel's non-buyers are exactly the users with no purchase event, so a report built by looking at purchases alone cannot see them — and a channel whose users never buy has to come back as 0, not vanish from the report and not read 1 because the only rows left were purchases.

users — one row per user. source is the channel that brought them in.

id name source signup_date
1 Aiden Cole organic 2023-05-01
2 Bianca Reyes paid_search 2023-05-02
3 Carlos Mora organic 2023-05-03
4 Delia Frank referral 2023-05-04
5 Ewan Blake paid_search 2023-05-05
6 Fiona Grey organic 2023-05-06
7 Gus Herrera referral 2023-05-07
8 Hana Ito paid_search 2023-05-08

events — one row per action. event_type is one of signup, view_product, add_to_cart or purchase.

id user_id event_type event_date
1 1 signup 2023-05-01
2 1 view_product 2023-05-01
3 1 add_to_cart 2023-05-02
4 1 purchase 2023-05-03
5 2 signup 2023-05-02
6 2 view_product 2023-05-02
7 3 signup 2023-05-03
8 3 view_product 2023-05-03
9 3 add_to_cart 2023-05-04
10 4 signup 2023-05-04
11 4 view_product 2023-05-05
12 4 add_to_cart 2023-05-05
13 4 purchase 2023-05-06
14 5 signup 2023-05-05
15 6 signup 2023-05-06
16 6 view_product 2023-05-06
17 7 signup 2023-05-07
18 7 view_product 2023-05-07
19 7 add_to_cart 2023-05-08
20 7 purchase 2023-05-09
21 8 signup 2023-05-08

Both tables already exist in the database — there is nothing to create or load.

Task: Write a query returning two columns, source and conversion_rate, with one row for each of the three channels. conversion_rate is the number of that channel's users who have at least one purchase event, divided by the number of users the channel brought in — a fraction between 0 and 1, not a percentage, rounded to 4 decimal places. A channel none of whose users bought anything comes back as 0 rather than being missing. Rows come back in alphabetical order of source.

Example output — shape only, on invented rates. A channel none of whose users bought would read 0, and one every user of which bought would read 1.

source conversion_rate
organic 0.25
paid_search 0.6
referral 0.8571

Sign in to solve this problem

Reading problems is free for everyone — solving them (Run, Submit, and tracking what you've solved) needs an account.

Sign in

Discussion

Sign in to join the discussion — reading is open to everyone.

Loading comments…