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:

  1. Selects user_id, page_id, and visit_time (aliased as interaction_time) from {{ ref("page_visits") }}, adding a literal 'visit' column as interaction_type
  2. Selects user_id, page_id, and like_time (aliased as interaction_time) from {{ ref("page_likes") }}, adding a literal 'like' column as interaction_type
  3. Selects user_id, page_id, and comment_time (aliased as interaction_time) from {{ ref("page_comments") }}, adding a literal 'comment' column as interaction_type
  4. 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_time column unifies the differently named timestamp columns, and interaction_type identifies which table each row originated from.

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 →