KatInterview

Evaluating Content Performance by Audience Relationship Type

SQL & DataModel answer
MediumModel answerSQL & Datasqlaggregationjoins

Last updated

Analyze video engagement and interaction rates segmented by viewer relationship types over a rolling 7-day window using SQL aggregations and joins.

You are tasked with analyzing social feed performance using two event tables: info_stream_views and post_action.

Table Schemas

info_stream_views

  • view_id (INTEGER): Unique primary key for the view event.
  • post_id (INTEGER): ID of the viewed post.
  • viewer_id (INTEGER): ID of the user who viewed the content.
  • creator_connection_type (VARCHAR): Relationship category between viewer and author ('friend' or 'unconnected').
  • view_duration_seconds (INTEGER): Duration spent watching the post in seconds.
  • view_date (DATE): Date of the view.

post_action

  • action_id (INTEGER): Unique primary key for the engagement event.
  • post_id (INTEGER): ID of the post acted upon.
  • action_type (VARCHAR): Type of interaction ('react', 'comment', 'share').
  • actor_id (INTEGER): ID of the user taking the action.
  • action_date (DATE): Date the action occurred.

Tasks

  1. High-Watch Unconnected Posts: Write a query that computes the count of unique post_id values where the total aggregate viewing duration by 'unconnected' users strictly exceeds 60 seconds within the last 7 days (view_date > CURRENT_DATE - INTERVAL '7 days' AND view_date <= CURRENT_DATE).

  2. Average Reactions by Connection Group: Write a query to compute the average number of 'react' actions received per post, categorized separately for posts seen in the last 7 days by 'friend' vs. 'unconnected' connection types. Ensure that posts in these viewing categories that received zero reactions are included in the average calculation.

Hints

Hint 1

For Task 1, distinguish between row-level filtering with WHERE and aggregate threshold filtering with HAVING.

Hint 2

For Task 2, avoid joining raw event logs directly to prevent fan-out; aggregate reaction counts per post in a Common Table Expression (CTE) first.

Hint 3

Remember to preserve posts with zero reactions using a LEFT JOIN and COALESCE.