Evaluating Content Performance by Audience Relationship Type
SQL & DataModel answerLast 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
-
High-Watch Unconnected Posts: Write a query that computes the count of unique
post_idvalues 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). -
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.
Model answer
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Clarifying Requirements & Modeling Assumptions
- Date Filtering: We assume a standard PostgreSQL trailing 7-day window:
date_col > CURRENT_DATE - INTERVAL '7 days' AND date_col <= CURRENT_DATE. - Post Classification: The attribute
creator_connection_typeis captured at the view event level, indicating the relationship between the viewer and the creator. If a post is viewed by both a friend and an unconnected user during the time window, the post belongs to both groups. - Zero Reactions: Posts that were viewed within the 7-day window but generated zero reactions must be included with a reaction count of 0 to ensure the average reaction metric is not artificially inflated.
Solution Strategy
Task 1: High-Watch Unconnected Posts
We need to filter for view events where creator_connection_type = 'unconnected' inside the date boundary. We aggregate by post_id and apply HAVING SUM(view_duration_seconds) > 60. Finally, we wrap this in a CTE or subquery to count the qualifying posts.
WITH high_watch_posts AS (
SELECT
post_id
FROM info_stream_views
WHERE creator_connection_type = 'unconnected'
AND view_date > CURRENT_DATE - INTERVAL '7 days'
AND view_date <= CURRENT_DATE
GROUP BY post_id
HAVING SUM(view_duration_seconds) > 60
)
SELECT
COUNT(*) AS total_high_watch_posts
FROM high_watch_posts;
Task 2: Average Reactions by Connection Group
Joining info_stream_views directly with post_action on post_id introduces a fan-out issue: if a post has 3 views and 2 reactions, a naive join creates $3 \times 2 = 6$ rows, duplicating action counts.
To avoid fan-out:
- Extract unique
(post_id, creator_connection_type)pairings frominfo_stream_viewswithin the target date window. - Aggregate
post_actionseparately to compute total'react'events perpost_idwithin the target date window. LEFT JOINthe unique post-category mapping to the pre-aggregated reaction totals.- Use
COALESCE(reactions, 0)and computeAVG().
WITH post_connection_map AS (
SELECT DISTINCT
post_id,
creator_connection_type
FROM info_stream_views
WHERE view_date > CURRENT_DATE - INTERVAL '7 days'
AND view_date <= CURRENT_DATE
AND creator_connection_type IN ('friend', 'unconnected')
),
reaction_counts AS (
SELECT
post_id,
COUNT(*) AS total_reactions
FROM post_action
WHERE action_type = 'react'
AND action_date > CURRENT_DATE - INTERVAL '7 days'
AND action_date <= CURRENT_DATE
GROUP BY post_id
)
SELECT
pcm.creator_connection_type,
AVG(1.0 * COALESCE(rc.total_reactions, 0)) AS avg_reactions_per_post,
COUNT(pcm.post_id) AS total_posts_in_category
FROM post_connection_map pcm
LEFT JOIN reaction_counts rc
ON pcm.post_id = rc.post_id
GROUP BY pcm.creator_connection_type;
Complexity & Edge Cases
- Time Complexity:
- Task 1 requires $O(V)$ scan time over filtered views plus $O(P)$ hash grouping overhead.
- Task 2 requires $O(V)$ time for view deduplication and $O(A)$ for action aggregation, followed by an $O(P)$ hash join, where $V$ is total views, $A$ is total actions, and $P$ is distinct posts.
- Null Safety:
COALESCE(rc.total_reactions, 0)guarantees posts with zero reactions contribute0toAVG()rather than being dropped or causing null propagation. - Integer Division: Multiplying by
1.0ensures floating-point division in SQL dialects that default to integer division (such as PostgreSQL or SQL Server).