Customer Churn Across Three Sources
Beginner Mode

Scenario

A company tracks customer churn across three separate data sources: user accounts, activity logs, and exit surveys. The activity logs contain duplicate records that must be cleaned before combining.

Task

Write a Snowflake SQL query that:

  1. Deduplicates {{ ref("activities") }} by removing rows with identical user_id, activity_date, and activity_type (keep one row per unique combination)
  2. Left joins the deduplicated activities and {{ ref("exit_surveys") }} onto {{ ref("accounts") }} using user_id
  3. Returns all seven columns and sorts by user_id ascending, then activity_date descending

Schema

accounts

Column Type Description
user_id String Unique user identifier
account_created_date Date Date the account was created
location String User's city

activities

Column Type Description
user_id String User identifier
activity_date Date Date of the activity
activity_type String Type of activity performed

exit_surveys

Column Type Description
user_id String User identifier
exit_date Date Date the user churned
exit_reason String Reason given for leaving

Example

accounts:

user_id account_created_date location
A01 2024-01-10 Portland
A02 2024-01-18 Denver
A03 2024-02-03 Atlanta
A04 2024-02-20 Seattle
A05 2024-03-01 Miami

activities (with duplicates):

user_id activity_date activity_type
A01 2024-03-12 Login
A01 2024-03-12 Login
A02 2024-03-18 Export
A02 2024-03-18 Export
A03 2024-04-01 Deploy
A04 2024-04-10 Login
A01 2024-04-15 Deploy

exit_surveys:

user_id exit_date exit_reason
A01 2024-05-01 Switched provider
A03 2024-05-10 Too expensive
A04 2024-05-15 Missing features

Expected Output:

user_id account_created_date location activity_date activity_type exit_date exit_reason
A01 2024-01-10 Portland 2024-04-15 Deploy 2024-05-01 Switched provider
A01 2024-01-10 Portland 2024-03-12 Login 2024-05-01 Switched provider
A02 2024-01-18 Denver 2024-03-18 Export NULL NULL
A03 2024-02-03 Atlanta 2024-04-01 Deploy 2024-05-10 Too expensive
A04 2024-02-20 Seattle 2024-04-10 Login 2024-05-15 Missing features
A05 2024-03-01 Miami NULL NULL NULL NULL

Note: Duplicate activity rows are reduced to one per unique combination. A01 has two distinct activities so appears twice. A02 has no exit survey (NULLs). A05 has no activities and no exit survey (NULLs in both). Results are sorted by user_id ASC, then activity_date DESC.

Quick Solution

Code Environment

Sign in or try as guest to run your code.

Sign In

Track

Question Difficulty Company Access
Need more practice in this area? Explore more questions →