KatInterview

Campaign Profit by Segment and Gala Seat Filling in SQL

SQL & DataModel answer
MediumModel answerSQL & Datasqlaggregationjoins

Last updated

Build per-campaign, per-segment fundraising metrics for August 2025 with fan-out-safe joins and cost allocation, pick the more profitable campaign, then choose extra gala invitees.

A charity runs several fundraising campaigns, and you need to report on how profitable each one was. You are given four tables.

Schema

  • donors(donor_id INT PRIMARY KEY, segment CHAR(1))
  • campaigns(campaign_id INT PRIMARY KEY, name VARCHAR, channel VARCHAR, fixed_cost DECIMAL, variable_cost_per_reach DECIMAL)
  • contacts(contact_id INT PRIMARY KEY, donor_id INT, campaign_id INT, reached_dt DATE)
  • donations(donation_id INT PRIMARY KEY, donor_id INT, campaign_id INT, amount DECIMAL, donation_dt DATE)

Sample data

donors

donor_idsegment
1H
2L
3L
4H
5L

campaigns

campaign_idnamechannelfixed_costvariable_cost_per_reach
10Gala Fallgala20000100
20Email Septonline80001

contacts

contact_iddonor_idcampaign_idreached_dt
1011102025-08-15
1022102025-08-15
1034102025-08-15
2013202025-08-31
2025202025-08-31

donations

donation_iddonor_idcampaign_idamountdonation_dt
30111010002025-08-16
3022102502025-08-16
401320502025-09-01
402520402025-09-01

Tasks

  1. For every campaign_id and donor segment, report these metrics for August 2025: total_reached, donors (distinct donors who gave), conversion_rate, gross_donations, variable_cost, fixed_cost, and net_revenue = gross_donations - (fixed_cost + variable_cost).
  2. Return a single row that shows which of the gala campaign and the online campaign has the higher net_revenue.
  3. Write a query that recommends the top 10 extra donors to invite to the gala. Rank candidates by their donation total over the last 12 months, and exclude anyone already contacted for the gala.

Rules and assumptions

  • For the gala, variable cost is charged per attendee, which means per contact row. Apply the same per-contact-row rule to every campaign.
  • Gala capacity is 100. Campaign 10 currently has fewer than 100 contact rows, so only the remaining seats can be filled.
  • A table donations_hist(donor_id, amount, donation_dt) holds historical giving for Task 3.
  • Some inputs are ambiguous, for example what "August" means for a donation made the day after a contact. State the interpretation you choose and explain why.

Expected: the SQL for all three tasks, the result on the sample data, and a short explanation of the pitfalls you avoided.

Hints

Hint 1

Joining contacts straight to donations repeats each gift once per contact row and inflates revenue. Collapse each fact table to one row per (campaign_id, donor_id) before joining.

Hint 2

Fixed cost belongs to the whole campaign, but the output has one row per segment. Split it across segments in proportion to how many donors each segment reached, so the pieces add back to the campaign total.

Hint 3

One online contact is on 2025-08-31 and its gifts arrive on 2025-09-01. Think about defining the month by when the donor was reached, not when they gave. For Task 3, use NOT EXISTS instead of NOT IN and cap the result at the seats still open.