KatInterview

Social Network Feed Analytics with SQL Aggregation

SQL & DataAuto-graded
MediumAuto-gradedSQL & Datasqldata-aggregationwindow-functions

Last updated

Build SQL queries to analyze user engagement patterns across friend and non-friend content, handling temporal aggregation and interaction scoring.

You're analyzing engagement metrics for a social platform. The database contains user activity across posts, feed views, and interactions.

Schema (PostgreSQL):

users(user_id INT, join_date DATE)
friendships(user_id INT, friend_id INT)  -- bidirectional; both edges stored
posts(post_id INT, author_id INT, created_at TIMESTAMP)
feed_impressions(imp_id INT, user_id INT, post_id INT, impression_ts TIMESTAMP)
interactions(int_id INT, user_id INT, post_id INT, type VARCHAR, interaction_ts TIMESTAMP)
-- type is one of: 'like', 'comment', 'share'

Sample Data:

users: (1, '2025-06-01'), (2, '2025-06-15'), (3, '2025-07-01')
friendships: (1,2), (2,1)
posts: (10, 2, '2025-08-01 10:00'), (11, 3, '2025-08-01 11:00'),
       (12, 2, '2025-08-02 09:00'), (13, 3, '2025-07-15 08:00')
feed_impressions: 
  (100, 1, 10, '2025-08-01 10:05'), (101, 1, 11, '2025-08-01 11:05'),
  (102, 1, 12, '2025-08-02 09:10'), (103, 2, 11, '2025-08-01 12:00'),
  (104, 1, 13, '2025-07-15 08:05')
interactions:
  (200, 1, 10, 'like', '2025-08-01 10:06'), (201, 1, 11, 'comment', '2025-08-01 11:06'),
  (202, 1, 12, 'share', '2025-08-02 09:12'), (203, 2, 11, 'like', '2025-08-01 12:05'),
  (204, 1, 13, 'like', '2025-07-15 08:06')

Write SQL queries for each task:

1. Daily Content Source Distribution
For each viewer and calendar date, calculate what proportion of their feed views came from friends versus non-friends. Tag each impression by checking if the post author is connected to the viewer via the friendships table.

2. Engagement Score by Content Origin
Calculate a weighted engagement metric per (viewer, date, content_origin) where content_origin is 'friend' or 'unconnected'. Use these weights: like=1, comment=3, share=5. Count all interaction types separately and compute the total weighted value. Feed views with no corresponding interaction contribute zero.

3. Daily Top Posts per User
For the date '2025-08-01', identify each user's top 2 posts by their engagement score (same weighting). Use DENSE_RANK() to include all ties at the boundary rank. Within ties, sort by post_id ascending for display.

4. Monthly Growth Analysis
Calculate month-over-month percentage change in total engagement score by content origin, comparing July 2025 to August 2025. Generate a complete calendar of months and handle cases where a month has no activity by treating it as zero score. Clearly state your handling of division by zero.

For all queries, assume UTC timestamps. Be precise about aggregation levels—ensure viewer-day grain where specified, not post-level.

Hints

Hint 1

When classifying impressions by relationship, use EXISTS rather than LEFT JOIN to avoid accidentally duplicating rows if the friendships table has unexpected entries.

Hint 2

A viewer might see the same post multiple times in one day and interact with it once. Pre-aggregate interactions to (viewer, post, date) grain before joining to impressions to avoid counting interactions multiple times.

Hint 3

For month-over-month calculations with sparse data, generate a complete calendar with CROSS JOIN to ensure both content types appear in both months, even if they had zero activity.