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.
Each row of `emails` is one user action on an email campaign. The table has the columns `id` (INTEGER, the primary key, unique id of the action row), `user_id` (INTEGER, not null, the user the action belongs to), `campaign_id` (INTEGER, not null, the campaign the email belongs to), `action` (VARCHAR(10), not null, one of 'Sent', 'Opened', 'Clicked') and `action_date` (DATE, not null, the date of the action). The same user can have several rows for the same campaign and action.
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.
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.
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 'Sent', 'Opened' and 'Clicked'.
- 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.
Tables
emails(id INTEGER, user_id INTEGER, campaign_id INTEGER, action VARCHAR(10), action_date DATE)
Hints
- One user can have several 'Sent' or 'Clicked' rows for the same campaign, and each user counts once per campaign and action.
- Check what type your division returns before you round it.