0
← Exercises/
Joins•2 of 12
JoinsIntermediate

Ad Campaign Attribution & ROI

Scenario

The performance marketing director needs an attribution report connecting advertising spend to confirmed closed-won revenue from the CRM deals table.

Task
Join `ad_campaigns` with `deals` to compute campaign revenue efficiency. Calculate: 1. `campaign_name` 2. `channel` 3. `budget_spent` 4. `revenue_generated`: Sum of `deal_value` for `'closed_won'` deals (coalesced to `0` if none) 5. `roas`: Return on Ad Spend, calculated as `ROUND(SUM(d.deal_value) / c.budget_spent, 2)` Only include campaigns where at least one closed-won deal exists. Order the output by `roas` in **descending** order.
Expected Output Format

Campaign performance metrics with campaign_name, channel, budget_spent, revenue_generated, and roas.

Input Tables & Sample Data0 tables
Loading database schema...
Need a hint?
query.sql|Local Sandbox
Loading SQL editor...
Click "Run" or press ⌘ + Enter to execute your query

About the “Ad Campaign Attribution & ROI” SQL Problem

Business Scenario

The performance marketing director needs an attribution report connecting advertising spend to confirmed closed-won revenue from the CRM deals table.

Expected Output & Criteria

Campaign performance metrics with campaign_name, channel, budget_spent, revenue_generated, and roas.

Database Table Schema (PostgreSQL DDL)

PostgreSQL 16 WASM

This challenge executes directly against the following relational schema in your browser:

CREATE TABLE ad_campaigns (
        campaign_id INT PRIMARY KEY,
        campaign_name VARCHAR(100) NOT NULL,
        channel VARCHAR(50) NOT NULL,
        budget_spent NUMERIC(10, 2) NOT NULL
      );

      CREATE TABLE deals (
        deal_id INT PRIMARY KEY,
        campaign_id INT REFERENCES ad_campaigns(campaign_id),
        deal_status VARCHAR(30) NOT NULL,
        deal_value NUMERIC(10, 2) NOT NULL
      );

Target SQL Clauses & Operators

JOINGROUP BYSUMROUND

Want more Joins practice?

Explore the full syllabus of 12 exercises, syntax guides, and interview pitfalls.

View Joins Track→