SQL
Lab
Meta Meta Interview Question 08

Ad
Revenue
by Campaign

Calculate total revenue per ad campaign.

Table Schema

Inspect
Table

interactive
ColumnType
ad_idinteger
campaign_idvarchar
revenuedecimal

Sample Data

Input
Output

Sample Input: ads
ad_idcampaign_idrevenue
1C1100
2C1200
3C2300
4C2150
5C3400
Expected Output
campaign_idtotal_revenue
C1300
C2450
C3400

SQL Editor

Run
Query

postgresql
Waiting for query

campaign_idtotal_revenue
C1300
C2450
C3400

Hints

Unlock
Clues

Hint 01: Identify the grouping level required by the output.
Hint 02: Aggregate with COUNT, SUM, AVG, or a window function as needed.
Hint 03: Filter after aggregation with HAVING or after ranking with an outer query.

Solution

Locked
Answer

Solution is locked until you decide to reveal it. Try the editor first, then open this when you want the reference answer.

SELECT campaign_id, SUM(revenue) AS total_revenue
FROM ads
GROUP BY campaign_id;

Explanation

Step By
Step

01

Read the expected output columns to determine the final grain.

02

Aggregate or rank the input rows to calculate the requested metric.

03

Filter, sort, and alias the final columns to match the output.

Related Questions

Keep
Solving