Combine User Interactions
Beginner Mode
Scenario
You are a web developer analyzing user behavior on your company's website. You have three tables tracking different types of user interactions (visits, likes, and comments), each with a differently named timestamp column.
Task
Write a Snowflake SQL query that:
- Selects
user_id,page_id, andvisit_time(aliased asinteraction_time) from{{ ref("page_visits") }}, adding a literal'visit'column asinteraction_type - Selects
user_id,page_id, andlike_time(aliased asinteraction_time) from{{ ref("page_likes") }}, adding a literal'like'column asinteraction_type - Selects
user_id,page_id, andcomment_time(aliased asinteraction_time) from{{ ref("page_comments") }}, adding a literal'comment'column asinteraction_type - Combines all three result sets into one unified dataset
Schema
page_visits
| Column | Type | Description |
|---|---|---|
| user_id | String | Unique identifier for the user |
| page_id | String | Unique identifier for the page |
| visit_time | Date | Time the page was visited |
page_likes
| Column | Type | Description |
|---|---|---|
| user_id | String | Unique identifier for the user |
| page_id | String | Unique identifier for the page |
| like_time | Date | Time the page was liked |
page_comments
| Column | Type | Description |
|---|---|---|
| user_id | String | Unique identifier for the user |
| page_id | String | Unique identifier for the page |
| comment_time | Date | Time the comment was made |
Example
page_visits:
| user_id | page_id | visit_time |
|---|---|---|
| A1 | X1 | 2024-03-01 |
| A2 | X2 | 2024-03-02 |
page_likes:
| user_id | page_id | like_time |
|---|---|---|
| A1 | X3 | 2024-03-01 |
| A3 | X1 | 2024-03-03 |
page_comments:
| user_id | page_id | comment_time |
|---|---|---|
| A2 | X3 | 2024-03-02 |
| A3 | X2 | 2024-03-04 |
Expected Output:
| user_id | page_id | interaction_time | interaction_type |
|---|---|---|---|
| A1 | X1 | 2024-03-01 | visit |
| A1 | X3 | 2024-03-01 | like |
| A2 | X2 | 2024-03-02 | visit |
| A2 | X3 | 2024-03-02 | comment |
| A3 | X1 | 2024-03-03 | like |
| A3 | X2 | 2024-03-04 | comment |
Note: Each row from the three source tables appears exactly once in the output. The
interaction_timecolumn unifies the differently named timestamp columns, andinteraction_typeidentifies which table each row originated from.
Code Environment
Sign in or try as guest to run your code.
Track
| Question | Difficulty | Company | Access |
|---|
Need more practice in this area? Explore more questions →
Meta