SQL Playground Mission: Channel Conversion Report
Rank marketing channels by purchase rate without dropping zero-conversion channels.
Conversion reports need a careful base. If you start from purchase events,
channels with no purchases vanish. Starting from sessions and using a
LEFT JOIN
keeps every channel in view.
This mission also uses distinct counts because a session can contain more than one event row. The result is a clean channel report: total sessions, purchasing sessions, and purchase rate.
select base.segment,
count(distinct base.id) as total,
count(distinct event.id) filter (where event.name = 'target') as converted,
round(100.0 * count(distinct event.id) filter (where event.name = 'target')
/ count(distinct base.id), 1) as conversion_rate
from base
left join event on event.id = base.id
group by base.segment Schema · Website Analytics Table · sessions 7 columns · 30 rows
One row per visit. Anonymous visits have a null user_id; organic and direct visits have a null campaign_id.
Schema Table · events 4 columns · 39 rows
Funnel events (signup, add_to_cart, checkout, purchase). Only purchase events carry a value.
Return one row per channel with channel, sessions,
purchasing_sessions, and purchase_rate. Sort by
purchase rate descending, then channel.
Run your query to inspect the result.
Expected answer
- Columns: channel, sessions, purchasing_sessions, purchase_rate.
- Rows: one per channel (5 rows).
- Social has the highest purchase rate at 37.5%.
Related
Nested SELECTs, IN, EXISTS, and correlation.
Ranking, LAG/LEAD, and running totals at a glance.
Practice ranking, running totals, and top row per group.
The default RANGE frame groups tied rows; use ROWS.
Filter window results in a CTE or with QUALIFY.
The WITH clause, chained steps, and CTE vs subquery.