Calculate each email campaign's click-through rate from unique sent and clicked users

Quick Overview

Write a SQL query that computes each email campaign's click-through rate as the share of unique users who clicked out of unique users who were sent the email. It tests conditional distinct counts in one GROUP BY, repeated action rows, and decimal division and rounding in PostgreSQL.

Calculate each email campaign's click-through rate from unique sent and clicked users

Company: Salesforce

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Each row of `emails` is one user action on an email campaign. For each `campaign_id`, compute `click_through_rate`: the percentage of unique users who `'Clicked'` out of the unique users who were `'Sent'` the email, rounded to 2 decimal places. Order the result by `campaign_id` ascending. ```hint Count people, not rows Check what happens when one user has several `'Sent'` or `'Clicked'` rows for the same campaign, and check what type your division returns. ``` ### Input Table(s) `emails` | column | type | meaning | |---|---|---| | `id` | INTEGER | Unique id of the action row. Primary key. | | `user_id` | INTEGER | User the action belongs to. Not null. | | `campaign_id` | INTEGER | Campaign the email belongs to. Not null. | | `action` | VARCHAR(10) | One of `'Sent'`, `'Opened'`, `'Clicked'`. Not null. | | `action_date` | DATE | Date of the action. Not null. | Key: `id` is the primary key. The same user can have several rows for the same campaign and action. ### Output Contract - Columns, in this order: `campaign_id`, `click_through_rate`. - One row per `campaign_id` present in `emails`. - `click_through_rate` is 100 times the number of distinct users with a `'Clicked'` row for the campaign, divided by the number of distinct users with a `'Sent'` row for the campaign, rounded to 2 decimal places. A campaign with no clicks has a rate of 0.00. - Sort by `campaign_id` ascending. ### Example `emails` | id | user_id | campaign_id | action | action_date | |---|---|---|---|---| | 1 | 1 | 1 | Sent | 2024-05-01 | | 2 | 2 | 1 | Sent | 2024-05-01 | | 3 | 3 | 1 | Sent | 2024-05-01 | | 4 | 1 | 1 | Opened | 2024-05-02 | | 5 | 1 | 1 | Clicked | 2024-05-02 | | 6 | 1 | 1 | Clicked | 2024-05-03 | | 7 | 2 | 1 | Opened | 2024-05-03 | | 8 | 4 | 2 | Sent | 2024-05-04 | | 9 | 4 | 2 | Sent | 2024-05-06 | | 10 | 5 | 2 | Sent | 2024-05-04 | | 11 | 4 | 2 | Clicked | 2024-05-07 | | 12 | 6 | 3 | Sent | 2024-05-05 | | 13 | 7 | 3 | Sent | 2024-05-05 | | 14 | 6 | 3 | Opened | 2024-05-06 | Expected result: | campaign_id | click_through_rate | |---|---| | 1 | 33.33 | | 2 | 50.00 | | 3 | 0.00 | Campaign 1 was sent to users 1, 2 and 3, and only user 1 clicked (twice), so the rate is 1 out of 3, or 33.33. Campaign 2 was sent to users 4 and 5; user 4 received it twice but counts once, and user 4 clicked, giving 1 out of 2, or 50.00. Campaign 3 has opens but no clicks, so its rate is 0.00. ### Constraints and Clarifications - `emails` has 1 to 10,000 rows. `user_id` is between 1 and 1000 and `campaign_id` is between 1 and 100. - Every `campaign_id` has at least one `'Sent'` row, so the denominator is never zero. - No column contains NULL, and `action` always holds one of the three exact strings shown. - Users are counted once per campaign and action, however many rows they have. - `'Opened'` rows and `action_date` do not affect the rate. A click counts whether or not the user has an `'Opened'` row. - The numerator counts every distinct user with a `'Clicked'` row for the campaign.

Overview: Write a SQL query that computes each email campaign's click-through rate as the share of unique users who clicked out of unique users who were sent the email. It tests conditional distinct counts in one GROUP BY, repeated action rows, and decimal division and rounding in PostgreSQL.

Salesforce logo
Salesforce
Nov 24, 2026
easyData ScientistOtherData Manipulation (SQL/Python)
0
0

Each row of emails is one user action on an email campaign. For each campaign_id, compute click_through_rate: the percentage of unique users who 'Clicked' out of the unique users who were 'Sent' the email, rounded to 2 decimal places. Order the result by campaign_id ascending.

Input Table(s)

emails

columntypemeaning
idINTEGERUnique id of the action row. Primary key.
user_idINTEGERUser the action belongs to. Not null.
campaign_idINTEGERCampaign the email belongs to. Not null.
actionVARCHAR(10)One of 'Sent', 'Opened', 'Clicked'. Not null.
action_dateDATEDate of the action. Not null.

Key: id is the primary key. The same user can have several rows for the same campaign and action.

Output Contract

  • Columns, in this order: campaign_id , click_through_rate .
  • One row per campaign_id present in emails .
  • click_through_rate is 100 times the number of distinct users with a 'Clicked' row for the campaign, divided by the number of distinct users with a 'Sent' row for the campaign, rounded to 2 decimal places. A campaign with no clicks has a rate of 0.00.
  • Sort by campaign_id ascending.

Example

emails

iduser_idcampaign_idactionaction_date
111Sent2024-05-01
221Sent2024-05-01
331Sent2024-05-01
411Opened2024-05-02
511Clicked2024-05-02
611Clicked2024-05-03
721Opened2024-05-03
842Sent2024-05-04
942Sent2024-05-06
1052Sent2024-05-04
1142Clicked2024-05-07
1263Sent2024-05-05
1373Sent2024-05-05
1463Opened2024-05-06

Expected result:

campaign_idclick_through_rate
133.33
250.00
30.00

Campaign 1 was sent to users 1, 2 and 3, and only user 1 clicked (twice), so the rate is 1 out of 3, or 33.33. Campaign 2 was sent to users 4 and 5; user 4 received it twice but counts once, and user 4 clicked, giving 1 out of 2, or 50.00. Campaign 3 has opens but no clicks, so its rate is 0.00.

Constraints and Clarifications

  • emails has 1 to 10,000 rows. user_id is between 1 and 1000 and campaign_id is between 1 and 100.
  • Every campaign_id has at least one 'Sent' row, so the denominator is never zero.
  • No column contains NULL, and action always holds one of the three exact strings shown.
  • Users are counted once per campaign and action, however many rows they have.
  • 'Opened' rows and action_date do not affect the rate. A click counts whether or not the user has an 'Opened' row.
  • The numerator counts every distinct user with a 'Clicked' row for the campaign.