Case 2: Evaluating campaigns with ROAS and CAC

A campaign can bring in plenty of revenue and hardly any new customers. Another wins lots of customers and looks weak on revenue. You calculate both measures and see why they run through different columns.

In short: ROAS is attributed revenue divided by cost and runs through orders.campaign_id. CAC is cost divided by new customers and runs through customers.campaign_id. Campaigns with no orders at all only show up with a LEFT JOIN, and the status filter then belongs in the ON.

The situation

Fourteen campaigns have run since January 2025, from the ongoing newsletter to half-yearly paid search and a short video ad test. The head of marketing is preparing the budget meeting and wants two answers for every campaign: did it bring in more than it cost, and what did a new customer cost through it?

The data

campaigns holds name, channel, start_date, end_date and cost. The cost is everything spent up to 31 October 2026, including campaigns that run longer. This is how campaigns and spend split across the channels:

SELECT channel, COUNT(*) AS campaigns, SUM(cost) AS cost
FROM campaigns
GROUP BY channel;
channelcampaignscost
Affiliate1240
Email3440
Paid Search41370
Paid Social61060

There are two links to results. orders.campaign_id names the campaign an order is attributed to, whether it is a first or a repeat purchase. customers.campaign_id names the campaign through which the customer was won. How a store attributes orders to campaigns (last click, first click, discount code) is its own decision; here the attribution has already been done.

Task 1: ROAS per campaign

Task For the budget meeting, the head of marketing needs the ROAS of every campaign. Show name, cost, revenue (the sum of amount over the completed orders attributed to the campaign) and roas (revenue divided by cost, rounded to 2 decimal places). Only campaigns with at least one completed order, highest ROAS first.

campaigns 14 rows, 6 columns
campaign_idnamechannelstart_dateend_datecost
1Paid search H1 2025Paid Search2025-01-012025-06-30120
2Paid social spring 2025Paid Social2025-03-012025-05-31160
3Email newsletter (ongoing)Email2025-01-012026-12-31360
4Affiliate program sports blogsAffiliate2025-01-012026-12-31240
5Paid search H2 2025Paid Search2025-07-012025-12-31420
6Paid social summer 2025Paid Social2025-06-152025-08-31180
7Email Black Friday week 2025Email2025-11-242025-12-0140
8Paid social Black Friday week 2025Paid Social2025-11-202025-12-01230
9Paid search H1 2026Paid Search2026-01-012026-06-30450
10Paid social fitness January 2026Paid Social2026-01-022026-01-31150
11Email spring sale 2026Email2026-03-022026-03-3140
12Paid search H2 2026Paid Search2026-07-012026-12-31380
13Paid social Sep/Oct 2026Paid Social2026-09-152026-10-31220
14Paid social video testPaid Social2026-05-042026-05-24120
orders 239 rows, 6 columns
order_idcustomer_idorder_datecampaign_idstatusamount
500112025-01-161completed29.8
500222025-01-24NULLcompleted139.7
500332025-01-28NULLcompleted49.9
500442025-02-014completed69.8
500552025-02-073completed89.9
500662025-02-14NULLcompleted24.9
500772025-02-25NULLcancelled24.9
500882025-03-032completed139.7
500992025-03-062completed34.9
501062025-03-13NULLcompleted24.9
5011102025-03-152completed19.9
5012112025-03-17NULLcompleted49.9
5013122025-03-293cancelled89.9
5014132025-03-304completed24.9
501522025-04-033completed24.9
5016142025-04-054completed79.9
501752025-04-063completed34.9
5018102025-04-10NULLcompleted89.9
5019152025-04-123completed114.8
5020162025-04-193completed99.9
5021172025-04-26NULLcancelled79.9
502272025-04-30NULLcompleted29.9
5023182025-05-03NULLcompleted34.9
5024192025-05-08NULLcompleted89.9
5025202025-05-123completed19.9
5026212025-05-172cancelled39.9
5027222025-05-18NULLcompleted79.9
5028142025-05-241completed89.9
5029232025-05-251completed29.8
5030162025-06-013cancelled27.9
5031242025-06-01NULLcompleted19.9
5032252025-06-093completed79.9
5033262025-06-156cancelled34.9
5034272025-06-163completed24.9
5035282025-06-186completed29.8
5036292025-06-256completed19.9
5037302025-07-06NULLcompleted19.8
5038312025-07-085completed24.9
5039322025-07-105completed119.7
5040332025-07-146completed49.9
5041342025-07-246completed34.9
5042352025-07-286cancelled89.9
5043242025-07-293completed34.9
5044362025-08-016completed104.8
504552025-08-053completed29.9
5046372025-08-06NULLcancelled119
504722025-08-073completed89.9
5048382025-08-09NULLcompleted9.9
5049392025-08-146completed19.9
5050402025-08-194completed89.9
5051412025-08-29NULLcompleted89.9
5052422025-08-293completed12.9
5053432025-09-03NULLcompleted24.9
5054442025-09-065completed34.9
5055232025-09-075completed99.9
5056382025-09-123completed25.8
5057452025-09-15NULLcompleted19.9
5058462025-09-185completed99.9
5059412025-09-19NULLcompleted34.9
5060472025-09-19NULLcompleted9.9
506152025-09-24NULLcompleted24.9
5062482025-09-29NULLcompleted79.9
5063492025-09-295completed29.9
5064502025-10-08NULLcompleted129
5065142025-10-10NULLcompleted86.7
5066512025-10-115completed119.7
5067162025-10-125completed99.9
5068522025-10-145completed119.7
5069342025-10-19NULLcompleted54.8
5070212025-10-20NULLcompleted49.9
5071422025-10-205completed16.9
5072532025-10-213completed89.9
5073542025-10-24NULLcompleted54.8
5074552025-10-295completed99.9
5075562025-11-04NULLcompleted34.9
5076572025-11-055completed114.8
5077272025-11-073completed12.9
5078312025-11-07NULLcompleted59.9
5079322025-11-07NULLcompleted27.9
5080582025-11-18NULLcompleted19.9
5081592025-11-235completed89.9
5082602025-11-238completed25.8
5083122025-11-247completed47.92
5084152025-11-248completed91.84
5085482025-11-247completed55.76
5086512025-11-247completed23.92
508712025-11-257completed31.92
508872025-11-25NULLcompleted47.92
5089612025-11-253completed23.92
509062025-11-267completed39.92
5091312025-11-268completed23.92
5092562025-11-268completed10.32
5093132025-11-277cancelled91.84
5094302025-11-277completed31.92
5095382025-11-287completed103.2
5096552025-11-288completed63.92
509792025-11-298completed27.92
509852025-11-308completed15.84
5099422025-11-303completed7.92
5100442025-11-307completed48.56
5101462025-11-307completed38.24
5102472025-11-307completed31.92
5103242025-12-017completed90.24
5104622025-12-018completed15.92
5105632025-12-08NULLcompleted19.8
5106642025-12-11NULLcompleted114.8
5107142025-12-12NULLcompleted29.9
5108652025-12-155completed89.7
5109662025-12-195completed104.8
5110672025-12-25NULLcompleted34.9
5111682025-12-26NULLcompleted59.9
5112422026-01-04NULLcompleted79.9
5113692026-01-0610completed16.9
5114702026-01-063completed19.9
511522026-01-099completed89.9
5116712026-01-169completed25.8
5117722026-01-179completed89.9
5118502026-01-18NULLcompleted19.9
5119732026-01-2310completed59.9
5120742026-01-26NULLcompleted19.8
5121752026-02-029completed119
5122762026-02-064completed24.9
5123772026-02-07NULLcompleted79.9
5124782026-02-13NULLcompleted9.9
5125792026-02-15NULLcompleted104.8
5126742026-02-18NULLcompleted49.9
5127802026-02-204completed19.9
5128682026-02-24NULLcompleted12.9
5129812026-02-259completed29.9
5130522026-02-28NULLcompleted59.9
5131382026-03-013completed89.9
5132822026-03-01NULLcompleted19.9
513342026-03-02NULLcompleted29.8
5134672026-03-0311completed39.9
513522026-03-059completed44.9
5136172026-03-05NULLcompleted79.9
5137312026-03-06NULLcompleted136.7
5138832026-03-07NULLcompleted49.9
5139702026-03-0811completed47.8
5140842026-03-113completed49.8
514172026-03-12NULLcompleted19.9
5142532026-03-1211completed19.9
5143852026-03-124completed22.8
514452026-03-13NULLcancelled104.8
5145162026-03-149completed34.9
5146572026-03-15NULLcompleted107.8
5147142026-03-1611completed24.9
5148322026-03-1611completed34.9
5149542026-03-17NULLcompleted34.9
5150862026-03-17NULLcompleted25.8
5151272026-03-18NULLcompleted9.9
5152872026-03-199completed119
5153722026-03-2211completed24.9
5154882026-04-014completed29.9
5155892026-04-059completed49.9
5156902026-04-059completed102.8
5157742026-04-063completed24.9
5158912026-04-119completed29.9
5159612026-04-12NULLcompleted79.9
5160922026-04-14NULLcompleted29.9
5161932026-04-19NULLcompleted29.9
5162942026-04-249completed29.9
5163952026-04-284completed44.9
5164962026-04-309completed129
5165922026-05-07NULLcompleted55.7
5166972026-05-104completed54.8
5167982026-05-119completed14.9
5168992026-05-134completed34.9
51691002026-05-189completed129
51701012026-05-194completed148.7
5171632026-05-233cancelled24.9
51721022026-05-25NULLcompleted44.9
5173782026-06-01NULLcancelled153.9
51741032026-06-01NULLcompleted99.9
5175422026-06-029completed129
51761042026-06-04NULLcompleted49.9
51771052026-06-08NULLcompleted19.9
5178502026-06-09NULLcompleted102.8
51791062026-06-09NULLcompleted119.7
51801072026-06-164cancelled29.7
51811082026-06-229completed12.9
5182792026-06-233completed99.9
51831092026-06-264completed129
51841102026-06-26NULLcancelled24.9
5185812026-06-28NULLcompleted99.9
51861112026-07-02NULLcompleted158.9
5187772026-07-04NULLcompleted114.7
51881122026-07-04NULLcompleted39.9
51891132026-07-094cancelled19.9
51901142026-07-14NULLcompleted49.9
5191832026-07-15NULLcompleted148.7
5192762026-07-17NULLcompleted34.9
5193842026-07-17NULLcompleted34.9
51941152026-07-19NULLcompleted79.9
5195742026-07-233completed33.8
5196922026-07-2712completed19.9
51971162026-07-2712completed27.9
51981172026-07-28NULLcompleted99.9
5199672026-07-303completed29.9
52001182026-08-03NULLcompleted119
52011192026-08-073completed102.8
52021202026-08-09NULLcompleted89.9
5203932026-08-153completed47.8
52041212026-08-15NULLcompleted27.9
52051222026-08-18NULLcompleted44.9
52061022026-08-243completed89.8
52071232026-08-2512completed142.7
52081242026-08-2712completed113.7
5209952026-08-2812completed99.9
5210922026-08-313completed29.8
52111252026-08-313completed19.9
52121102026-09-01NULLcompleted42.7
52131262026-09-033completed29.9
52141272026-09-07NULLcompleted29.8
52151282026-09-13NULLcompleted24.9
52161292026-09-1913completed89.9
52171302026-09-20NULLcompleted19.9
52181172026-09-213completed96.8
52191312026-09-223completed89.9
5220502026-09-23NULLcompleted44.9
5221912026-09-23NULLcompleted89.9
5222922026-09-233completed89.9
52231322026-09-2612completed89.9
52241152026-10-03NULLcompleted25.8
52251332026-10-0413cancelled49.9
5226632026-10-05NULLcompleted24.9
52271342026-10-0513completed27.9
52281062026-10-08NULLcompleted29.9
52291192026-10-08NULLcompleted24.9
52301352026-10-1012completed24.9
5231952026-10-13NULLcompleted24.9
52321022026-10-13NULLcompleted49.9
5233982026-10-14NULLcompleted14.9
52341362026-10-164completed24.9
52351372026-10-203completed34.9
52361382026-10-2213completed34.9
52371392026-10-27NULLcompleted89.8
52381402026-10-293completed19.9
52391122026-10-31NULLcompleted104.8

How it works

ROAS stands for return on ad spend: revenue attributed to a campaign divided by its cost. A ROAS of 2 means two units of revenue for every unit spent on ads. You join campaigns to orders on campaign_id, keep completed orders and group by campaign. Because cost has a single value per campaign, you can use it directly after GROUP BY c.campaign_id.

The email campaigns come out on top, the Black Friday week send at 13.59. That is the nature of the channel: email costs little and reaches people who have bought before. ROAS is a revenue measure. It says nothing about margin, and nothing about whether some of those orders would have arrived without the campaign.

Task 2: campaigns that do not pay for themselves

Task Which campaigns brought in less revenue than they cost (ROAS below 1)? Campaigns with no orders at all must be included. Show name, cost and revenue (the sum of amount over the attributed completed orders, 0 if there are none).

campaigns 14 rows, 6 columns
campaign_idnamechannelstart_dateend_datecost
1Paid search H1 2025Paid Search2025-01-012025-06-30120
2Paid social spring 2025Paid Social2025-03-012025-05-31160
3Email newsletter (ongoing)Email2025-01-012026-12-31360
4Affiliate program sports blogsAffiliate2025-01-012026-12-31240
5Paid search H2 2025Paid Search2025-07-012025-12-31420
6Paid social summer 2025Paid Social2025-06-152025-08-31180
7Email Black Friday week 2025Email2025-11-242025-12-0140
8Paid social Black Friday week 2025Paid Social2025-11-202025-12-01230
9Paid search H1 2026Paid Search2026-01-012026-06-30450
10Paid social fitness January 2026Paid Social2026-01-022026-01-31150
11Email spring sale 2026Email2026-03-022026-03-3140
12Paid search H2 2026Paid Search2026-07-012026-12-31380
13Paid social Sep/Oct 2026Paid Social2026-09-152026-10-31220
14Paid social video testPaid Social2026-05-042026-05-24120
orders 239 rows, 6 columns
order_idcustomer_idorder_datecampaign_idstatusamount
500112025-01-161completed29.8
500222025-01-24NULLcompleted139.7
500332025-01-28NULLcompleted49.9
500442025-02-014completed69.8
500552025-02-073completed89.9
500662025-02-14NULLcompleted24.9
500772025-02-25NULLcancelled24.9
500882025-03-032completed139.7
500992025-03-062completed34.9
501062025-03-13NULLcompleted24.9
5011102025-03-152completed19.9
5012112025-03-17NULLcompleted49.9
5013122025-03-293cancelled89.9
5014132025-03-304completed24.9
501522025-04-033completed24.9
5016142025-04-054completed79.9
501752025-04-063completed34.9
5018102025-04-10NULLcompleted89.9
5019152025-04-123completed114.8
5020162025-04-193completed99.9
5021172025-04-26NULLcancelled79.9
502272025-04-30NULLcompleted29.9
5023182025-05-03NULLcompleted34.9
5024192025-05-08NULLcompleted89.9
5025202025-05-123completed19.9
5026212025-05-172cancelled39.9
5027222025-05-18NULLcompleted79.9
5028142025-05-241completed89.9
5029232025-05-251completed29.8
5030162025-06-013cancelled27.9
5031242025-06-01NULLcompleted19.9
5032252025-06-093completed79.9
5033262025-06-156cancelled34.9
5034272025-06-163completed24.9
5035282025-06-186completed29.8
5036292025-06-256completed19.9
5037302025-07-06NULLcompleted19.8
5038312025-07-085completed24.9
5039322025-07-105completed119.7
5040332025-07-146completed49.9
5041342025-07-246completed34.9
5042352025-07-286cancelled89.9
5043242025-07-293completed34.9
5044362025-08-016completed104.8
504552025-08-053completed29.9
5046372025-08-06NULLcancelled119
504722025-08-073completed89.9
5048382025-08-09NULLcompleted9.9
5049392025-08-146completed19.9
5050402025-08-194completed89.9
5051412025-08-29NULLcompleted89.9
5052422025-08-293completed12.9
5053432025-09-03NULLcompleted24.9
5054442025-09-065completed34.9
5055232025-09-075completed99.9
5056382025-09-123completed25.8
5057452025-09-15NULLcompleted19.9
5058462025-09-185completed99.9
5059412025-09-19NULLcompleted34.9
5060472025-09-19NULLcompleted9.9
506152025-09-24NULLcompleted24.9
5062482025-09-29NULLcompleted79.9
5063492025-09-295completed29.9
5064502025-10-08NULLcompleted129
5065142025-10-10NULLcompleted86.7
5066512025-10-115completed119.7
5067162025-10-125completed99.9
5068522025-10-145completed119.7
5069342025-10-19NULLcompleted54.8
5070212025-10-20NULLcompleted49.9
5071422025-10-205completed16.9
5072532025-10-213completed89.9
5073542025-10-24NULLcompleted54.8
5074552025-10-295completed99.9
5075562025-11-04NULLcompleted34.9
5076572025-11-055completed114.8
5077272025-11-073completed12.9
5078312025-11-07NULLcompleted59.9
5079322025-11-07NULLcompleted27.9
5080582025-11-18NULLcompleted19.9
5081592025-11-235completed89.9
5082602025-11-238completed25.8
5083122025-11-247completed47.92
5084152025-11-248completed91.84
5085482025-11-247completed55.76
5086512025-11-247completed23.92
508712025-11-257completed31.92
508872025-11-25NULLcompleted47.92
5089612025-11-253completed23.92
509062025-11-267completed39.92
5091312025-11-268completed23.92
5092562025-11-268completed10.32
5093132025-11-277cancelled91.84
5094302025-11-277completed31.92
5095382025-11-287completed103.2
5096552025-11-288completed63.92
509792025-11-298completed27.92
509852025-11-308completed15.84
5099422025-11-303completed7.92
5100442025-11-307completed48.56
5101462025-11-307completed38.24
5102472025-11-307completed31.92
5103242025-12-017completed90.24
5104622025-12-018completed15.92
5105632025-12-08NULLcompleted19.8
5106642025-12-11NULLcompleted114.8
5107142025-12-12NULLcompleted29.9
5108652025-12-155completed89.7
5109662025-12-195completed104.8
5110672025-12-25NULLcompleted34.9
5111682025-12-26NULLcompleted59.9
5112422026-01-04NULLcompleted79.9
5113692026-01-0610completed16.9
5114702026-01-063completed19.9
511522026-01-099completed89.9
5116712026-01-169completed25.8
5117722026-01-179completed89.9
5118502026-01-18NULLcompleted19.9
5119732026-01-2310completed59.9
5120742026-01-26NULLcompleted19.8
5121752026-02-029completed119
5122762026-02-064completed24.9
5123772026-02-07NULLcompleted79.9
5124782026-02-13NULLcompleted9.9
5125792026-02-15NULLcompleted104.8
5126742026-02-18NULLcompleted49.9
5127802026-02-204completed19.9
5128682026-02-24NULLcompleted12.9
5129812026-02-259completed29.9
5130522026-02-28NULLcompleted59.9
5131382026-03-013completed89.9
5132822026-03-01NULLcompleted19.9
513342026-03-02NULLcompleted29.8
5134672026-03-0311completed39.9
513522026-03-059completed44.9
5136172026-03-05NULLcompleted79.9
5137312026-03-06NULLcompleted136.7
5138832026-03-07NULLcompleted49.9
5139702026-03-0811completed47.8
5140842026-03-113completed49.8
514172026-03-12NULLcompleted19.9
5142532026-03-1211completed19.9
5143852026-03-124completed22.8
514452026-03-13NULLcancelled104.8
5145162026-03-149completed34.9
5146572026-03-15NULLcompleted107.8
5147142026-03-1611completed24.9
5148322026-03-1611completed34.9
5149542026-03-17NULLcompleted34.9
5150862026-03-17NULLcompleted25.8
5151272026-03-18NULLcompleted9.9
5152872026-03-199completed119
5153722026-03-2211completed24.9
5154882026-04-014completed29.9
5155892026-04-059completed49.9
5156902026-04-059completed102.8
5157742026-04-063completed24.9
5158912026-04-119completed29.9
5159612026-04-12NULLcompleted79.9
5160922026-04-14NULLcompleted29.9
5161932026-04-19NULLcompleted29.9
5162942026-04-249completed29.9
5163952026-04-284completed44.9
5164962026-04-309completed129
5165922026-05-07NULLcompleted55.7
5166972026-05-104completed54.8
5167982026-05-119completed14.9
5168992026-05-134completed34.9
51691002026-05-189completed129
51701012026-05-194completed148.7
5171632026-05-233cancelled24.9
51721022026-05-25NULLcompleted44.9
5173782026-06-01NULLcancelled153.9
51741032026-06-01NULLcompleted99.9
5175422026-06-029completed129
51761042026-06-04NULLcompleted49.9
51771052026-06-08NULLcompleted19.9
5178502026-06-09NULLcompleted102.8
51791062026-06-09NULLcompleted119.7
51801072026-06-164cancelled29.7
51811082026-06-229completed12.9
5182792026-06-233completed99.9
51831092026-06-264completed129
51841102026-06-26NULLcancelled24.9
5185812026-06-28NULLcompleted99.9
51861112026-07-02NULLcompleted158.9
5187772026-07-04NULLcompleted114.7
51881122026-07-04NULLcompleted39.9
51891132026-07-094cancelled19.9
51901142026-07-14NULLcompleted49.9
5191832026-07-15NULLcompleted148.7
5192762026-07-17NULLcompleted34.9
5193842026-07-17NULLcompleted34.9
51941152026-07-19NULLcompleted79.9
5195742026-07-233completed33.8
5196922026-07-2712completed19.9
51971162026-07-2712completed27.9
51981172026-07-28NULLcompleted99.9
5199672026-07-303completed29.9
52001182026-08-03NULLcompleted119
52011192026-08-073completed102.8
52021202026-08-09NULLcompleted89.9
5203932026-08-153completed47.8
52041212026-08-15NULLcompleted27.9
52051222026-08-18NULLcompleted44.9
52061022026-08-243completed89.8
52071232026-08-2512completed142.7
52081242026-08-2712completed113.7
5209952026-08-2812completed99.9
5210922026-08-313completed29.8
52111252026-08-313completed19.9
52121102026-09-01NULLcompleted42.7
52131262026-09-033completed29.9
52141272026-09-07NULLcompleted29.8
52151282026-09-13NULLcompleted24.9
52161292026-09-1913completed89.9
52171302026-09-20NULLcompleted19.9
52181172026-09-213completed96.8
52191312026-09-223completed89.9
5220502026-09-23NULLcompleted44.9
5221912026-09-23NULLcompleted89.9
5222922026-09-233completed89.9
52231322026-09-2612completed89.9
52241152026-10-03NULLcompleted25.8
52251332026-10-0413cancelled49.9
5226632026-10-05NULLcompleted24.9
52271342026-10-0513completed27.9
52281062026-10-08NULLcompleted29.9
52291192026-10-08NULLcompleted24.9
52301352026-10-1012completed24.9
5231952026-10-13NULLcompleted24.9
52321022026-10-13NULLcompleted49.9
5233982026-10-14NULLcompleted14.9
52341362026-10-164completed24.9
52351372026-10-203completed34.9
52361382026-10-2213completed34.9
52371392026-10-27NULLcompleted89.8
52381402026-10-293completed19.9
52391122026-10-31NULLcompleted104.8

This exercise is part of Premium.

Premium unlocks every exercise in every lesson, unlimited practice with fresh tasks and the final test with certificate.

See Premium Already Premium? Log in

The first exercise in this lesson is free.

How it works

The worst campaign is the one without a single order, and that is exactly the one a plain JOIN hides. With a LEFT JOIN every campaign stays; where nothing matches, the order columns are NULL. SUM over nothing but NULL returns NULL, and COALESCE(…, 0) turns it into 0 (more in the NULL lesson). HAVING compares the result with the cost after grouping.

Task 3: cost per new customer

Task What did each campaign pay for a new customer? The campaign a customer was won through is stored in customers.campaign_id. Show name, cost, new_customers (the number of customers won that way) and cac (cost divided by new_customers, rounded to 2 decimal places), only for campaigns with at least one new customer.

campaigns 14 rows, 6 columns
campaign_idnamechannelstart_dateend_datecost
1Paid search H1 2025Paid Search2025-01-012025-06-30120
2Paid social spring 2025Paid Social2025-03-012025-05-31160
3Email newsletter (ongoing)Email2025-01-012026-12-31360
4Affiliate program sports blogsAffiliate2025-01-012026-12-31240
5Paid search H2 2025Paid Search2025-07-012025-12-31420
6Paid social summer 2025Paid Social2025-06-152025-08-31180
7Email Black Friday week 2025Email2025-11-242025-12-0140
8Paid social Black Friday week 2025Paid Social2025-11-202025-12-01230
9Paid search H1 2026Paid Search2026-01-012026-06-30450
10Paid social fitness January 2026Paid Social2026-01-022026-01-31150
11Email spring sale 2026Email2026-03-022026-03-3140
12Paid search H2 2026Paid Search2026-07-012026-12-31380
13Paid social Sep/Oct 2026Paid Social2026-09-152026-10-31220
14Paid social video testPaid Social2026-05-042026-05-24120
customers 140 rows, 6 columns
customer_idnamecitysignup_datechannelcampaign_id
1Sophia CampbellLeeds2025-01-16Paid Search1
2Aiden AllenLeeds2025-01-24OrganicNULL
3Aiden MillerAustin2025-01-28OrganicNULL
4Isla WrightGlasgow2025-02-01Affiliate4
5Henry GarciaGlasgow2025-02-07Email3
6Ryan ThomasPortland2025-02-14OrganicNULL
7Priya MurphyBristol2025-02-25OrganicNULL
8Chloe WilsonLondon2025-03-03Paid Social2
9Amelia EvansManchester2025-03-06Paid Social2
10Jack WilsonSeattle2025-03-15Paid Social2
11Grace CampbellAustin2025-03-17OrganicNULL
12Ava LewisAustin2025-03-29Email3
13Ruby MurphyLeeds2025-03-30Affiliate4
14Samuel KhanAtlanta2025-04-05Affiliate4
15Ruby MillerLeeds2025-04-12Email3
16Noah ThomasLondon2025-04-19Email3
17Leo DaviesDenver2025-04-26OrganicNULL
18Leo WrightChicago2025-05-03OrganicNULL
19Mia TaylorGlasgow2025-05-08OrganicNULL
20Liam RobinsonSeattle2025-05-12Email3
21Ella EvansDenver2025-05-17Paid Social2
22Daniel ScottBoston2025-05-18OrganicNULL
23Amelia WrightManchester2025-05-25Paid Search1
24Freya DaviesAtlanta2025-06-01OrganicNULL
25Noah TaylorBoston2025-06-09Email3
26Ava MillerSeattle2025-06-15Paid Social6
27Lucas WilsonAustin2025-06-16Email3
28Liam KhanPortland2025-06-18Paid Social6
29Lucas WrightGlasgow2025-06-25Paid Social6
30Noah MurphyDenver2025-07-06OrganicNULL
31Grace KhanBristol2025-07-08Paid Search5
32Liam DaviesSeattle2025-07-10Paid Search5
33Mason HallSeattle2025-07-14Paid Social6
34Ethan KhanPortland2025-07-24Paid Social6
35Jack ClarkeLeeds2025-07-28Paid Social6
36Ethan CampbellSeattle2025-08-01Paid Social6
37Harper KingBoston2025-08-06OrganicNULL
38Freya ScottManchester2025-08-09OrganicNULL
39Daniel JohnsonManchester2025-08-14Paid Social6
40Ruby WilsonAtlanta2025-08-19Affiliate4
41Zoe KhanAtlanta2025-08-29OrganicNULL
42Oliver YoungAustin2025-08-29Email3
43Leo KingGlasgow2025-09-03OrganicNULL
44Noah AllenBristol2025-09-06Paid Search5
45Oliver LewisAustin2025-09-15OrganicNULL
46James WalkerAustin2025-09-18Paid Search5
47Ava DaviesSeattle2025-09-19OrganicNULL
48Leo AllenLondon2025-09-29OrganicNULL
49James HallSeattle2025-09-29Paid Search5
50Aiden ThomasBoston2025-10-08OrganicNULL
51Oliver AllenAustin2025-10-11Paid Search5
52Daniel ClarkeGlasgow2025-10-14Paid Search5
53Liam HarrisSeattle2025-10-21Email3
54Lucas CampbellManchester2025-10-24OrganicNULL
55Freya BrownLondon2025-10-29Paid Search5
56Freya YoungSeattle2025-11-04OrganicNULL
57Sophia LewisPortland2025-11-05Paid Search5
58Freya EvansBristol2025-11-18OrganicNULL
59Leo PatelLondon2025-11-23Paid Search5
60Ruby ThomasGlasgow2025-11-23Paid Social8
61Amelia TaylorManchester2025-11-25Email3
62Liam MurphyDenver2025-12-01Paid Social8
63Daniel WalkerChicago2025-12-08OrganicNULL
64Zoe KingManchester2025-12-11OrganicNULL
65James ClarkeLeeds2025-12-15Paid Search5
66Samuel KingGlasgow2025-12-19Paid Search5
67Leo MillerDenver2025-12-25OrganicNULL
68Sophia EvansPortland2025-12-26OrganicNULL
69Sophia MurphyAustin2026-01-06Paid Social10
70Olivia KhanAustin2026-01-06Email3
71James ScottLeeds2026-01-16Paid Search9
72Noah SmithChicago2026-01-17Paid Search9
73Liam ScottLeeds2026-01-23Paid Social10
74Priya ScottPortland2026-01-26OrganicNULL
75Jack DaviesAustin2026-02-02Paid Search9
76Priya CampbellGlasgow2026-02-06Affiliate4
77Olivia AllenSeattle2026-02-07OrganicNULL
78Lucas YoungAustin2026-02-13OrganicNULL
79Emma GarciaBristol2026-02-15OrganicNULL
80Grace BrownLondon2026-02-20Affiliate4
81Zoe TaylorSeattle2026-02-25Paid Search9
82Ava BrownManchester2026-03-01OrganicNULL
83Ryan WilsonSeattle2026-03-07OrganicNULL
84Liam WrightAustin2026-03-11Email3
85Olivia TaylorSeattle2026-03-12Affiliate4
86Ethan EvansBoston2026-03-17OrganicNULL
87Ella ScottBoston2026-03-19Paid Search9
88Daniel HallGlasgow2026-04-01Affiliate4
89Chloe ThomasSeattle2026-04-05Paid Search9
90Harper TaylorDenver2026-04-05Paid Search9
91Ethan KingAustin2026-04-11Paid Search9
92Leo CampbellAustin2026-04-14OrganicNULL
93Noah YoungAtlanta2026-04-19OrganicNULL
94Noah JohnsonDenver2026-04-24Paid Search9
95Daniel EvansGlasgow2026-04-28Affiliate4
96Ella WilsonLondon2026-04-30Paid Search9
97Grace AllenBristol2026-05-10Affiliate4
98Jack SmithBristol2026-05-11Paid Search9
99Grace EvansPortland2026-05-13Affiliate4
100Isla ClarkeLondon2026-05-18Paid Search9
101Daniel AllenSeattle2026-05-19Affiliate4
102Zoe BrownLondon2026-05-25OrganicNULL
103Daniel LewisAtlanta2026-06-01OrganicNULL
104Isla MillerSeattle2026-06-04OrganicNULL
105Daniel WrightManchester2026-06-08OrganicNULL
106Zoe WrightGlasgow2026-06-09OrganicNULL
107Chloe WalkerAustin2026-06-16Affiliate4
108Chloe ScottAustin2026-06-22Paid Search9
109Henry EvansGlasgow2026-06-26Affiliate4
110Ryan ScottGlasgow2026-06-26OrganicNULL
111Ruby GarciaBoston2026-07-02OrganicNULL
112Ryan YoungManchester2026-07-04OrganicNULL
113Priya WalkerDenver2026-07-09Affiliate4
114Chloe HallChicago2026-07-14OrganicNULL
115Jack CampbellManchester2026-07-19OrganicNULL
116Harper ScottManchester2026-07-27Paid Search12
117Freya WrightChicago2026-07-28OrganicNULL
118Jack HallGlasgow2026-08-03OrganicNULL
119Chloe MillerManchester2026-08-07Email3
120Oliver GarciaBoston2026-08-09OrganicNULL
121Daniel KingPortland2026-08-15OrganicNULL
122Ruby HarrisChicago2026-08-18OrganicNULL
123Ruby LewisPortland2026-08-25Paid Search12
124Henry HarrisManchester2026-08-27Paid Search12
125Ethan SmithBristol2026-08-31Email3
126Emma LewisBristol2026-09-03Email3
127Zoe EvansBristol2026-09-07OrganicNULL
128Ethan JohnsonBristol2026-09-13OrganicNULL
129Ella LewisDenver2026-09-19Paid Social13
130Olivia RobinsonSeattle2026-09-20OrganicNULL
131Amelia KingBoston2026-09-22Email3
132Oliver TaylorGlasgow2026-09-26Paid Search12
133James AllenSeattle2026-10-04Paid Social13
134Henry KhanAtlanta2026-10-05Paid Social13
135Mia WrightManchester2026-10-10Paid Search12
136Mia JohnsonLondon2026-10-16Affiliate4
137Grace DaviesLeeds2026-10-20Email3
138Liam PatelBristol2026-10-22Paid Social13
139Oliver WrightSeattle2026-10-27OrganicNULL
140Henry AllenSeattle2026-10-29Email3

This exercise is part of Premium.

Premium unlocks every exercise in every lesson, unlimited practice with fresh tasks and the final test with certificate.

See Premium Already Premium? Log in

The first exercise in this lesson is free.

How it works

CAC stands for customer acquisition cost: cost divided by the number of customers won. This time the count comes from customers, not from orders. The affiliate program comes to 15 per new customer, the Black Friday paid social campaign to 115. The two email promotions do not appear at all: they brought in orders, but not one new customer.

Common mistakes

Status filter in WHERE after a LEFT JOIN

WHERE o.status = 'completed' throws away the row of the campaign without orders, because its status is NULL. The LEFT JOIN quietly turns into a plain JOIN. The exercise says: "Your query returns only 2 rows. Wanted: 3 rows."

Counting cancellations

Without a status filter, a cancelled order from the Sep/Oct 2026 campaign counts as revenue in task 2. The row count is right, the values are not.

CAC from the orders table

Joining orders instead of customers in task 3 gives cost per order (CPO) and returns 13 rows instead of 11, because the email promotions show up as well. The exercise asks: "Are you grouping by too many columns, or is a condition missing in WHERE or HAVING?"

Check your result

  • Task 1: 13 campaigns, with Email Black Friday week 2025 at the top (cost 40, revenue 543.52, ROAS 13.59) and Paid social fitness January 2026 at the bottom with 0.51.
  • Task 2: three campaigns, including the video test with a cost of 120 and revenue of 0.
  • Task 3: 11 campaigns. The new customers add up to 86, which is every customer with a campaign; the 54 organic customers are missing.

In MySQL, PostgreSQL and SQL Server

LEFT JOIN, COALESCE and HAVING work the same in all four systems. Check the data type of the cost column in your own tables: if it is stored as a whole number, PostgreSQL and SQL Server divide cost / COUNT(*) as integers, MySQL does not.

FAQ

What ROAS makes a campaign worth it?

That depends on margin. If 50 out of every 100 in revenue is left after the cost of goods, only a ROAS above 2 covers the ad spend. There is no universal threshold. Case 6 shows how to calculate the margin yourself.

What is the difference between CAC and CPO?

CAC divides cost by new customers, CPO by all attributed orders, repeat purchases included. A campaign aimed at existing customers therefore has a good CPO and no CAC at all.

Why does the Sep/Oct 2026 campaign have a poor ROAS?

It only ended on the last day of the data, and one of its orders was cancelled. Late orders are still missing. Campaigns that are still running are better judged again once they have finished.

Next up, case 3: customer retention

A customer is only worth a lot if they come back. You measure the repeat rate by channel and build cohorts with a 90-day window.

Start case 3

Write to us

Choose language

Select your preferred language