SQL Playground Mission: Campaign Revenue Report
Start from campaigns, keep zero rows visible, and calculate purchase revenue without dropping quiet campaigns.
Campaign reports should show quiet campaigns too. If you start from sessions
or purchases, a campaign with no traffic disappears. Starting from
campaigns
and using LEFT JOINs keeps the full marketing list visible.
This mission combines three common metrics: sessions, sessions that purchased, and purchase revenue attributed to sessions carrying that campaign id. It is an attribution report, not a measure of incremental revenue caused by the campaign.
select campaign_name,
count(distinct session_id) as sessions,
count(distinct session_id) filter (where event_name = 'purchase') as buyers,
coalesce(sum(value) filter (where event_name = 'purchase'), 0) as revenue
from joined_campaign_events
group by campaign_name Schema · Website Analytics Table · campaigns 5 columns · 5 rows
Marketing campaigns with their channel and UTM tags.
Schema 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 campaign with campaign_name, channel, distinct sessions, distinct purchasing_sessions, and purchase_revenue. Keep campaigns with no sessions or purchases. Sort by purchase_revenue descending, then campaign_name.
Run your query to inspect the result.
Expected answer
- Columns: campaign_name, channel, sessions, purchasing_sessions, purchase_revenue.
- Rows: one row per campaign (5 rows).
- Social Promo has 8 sessions, 3 purchasing sessions, and 146.35 in purchase revenue.
Related
INNER, LEFT, RIGHT, FULL, and CROSS joins.
Join types and common join patterns.
Practice join keys, left joins, duplicate rows, and debugging.
One-to-many joins can inflate totals.
Qualify shared columns with a table alias.
Nested SELECTs, IN, EXISTS, and correlation.