cswork / libraryOpen practice workspace

Coding questions · Meta

Aggregate Netflix metrics in SQL

This question evaluates a candidate's proficiency in SQL data aggregation and construction of cumulative metrics for streaming analytics, including handling alignment of records when identifiers may appear in only one dataset.

Question

Netflix video-streaming analytics SQL:

Write a simple aggregation (e.g., total watch-time per day).

Build a cumulative metric: today’s metric = today + yesterday, but IDs can exist in only one side; use a FULL JOIN to align.

Daily total watch-time

From the Netflix-style watch_events table, compute the total watch-time per calendar date. Return one row per date with columns watch_date and total_watch_seconds, ordered by watch_date ascending.

Table watch_events

columntypeconstraints
event_idINTEGERPRIMARY KEY, NOT NULL
user_idINTEGERNOT NULL
content_idINTEGERNOT NULL
started_atTIMESTAMPNOT NULL
watch_secondsINTEGERNOT NULL

Sample data:

content_idevent_idstarted_atuser_idwatch_seconds
10112025-01-01 08:15:0011800
10222025-01-01 09:00:0011200
10132025-01-01 20:20:0022400
10342025-01-01 12:00:0033600
10152025-01-02 07:30:0011500
10462025-01-02 19:45:002600
10572025-01-02 21:10:002900
10682025-01-02 10:05:0042700
10792025-01-02 14:30:0051200

Expected output

Total watch-time per date across all users and content.

total_watch_secondswatch_date
90002025-01-01
69002025-01-02

Hints

  1. Convert started_at to a date to form the per-day buckets.
  2. SUM watch_seconds and GROUP BY the date column.

Two-day per-user cumulative watch-time with FULL JOIN

For a given 'today' date, compute each user's two-day watch-time equal to today's watch-time plus yesterday's watch-time. Users may appear on only one of the two days, so use a FULL OUTER JOIN to align per-user totals for the two dates. Using today = 2025-01-02, return user_id and two_day_watch_seconds.

Table watch_events

columntypeconstraints
event_idINTEGERPRIMARY KEY, NOT NULL
user_idINTEGERNOT NULL
content_idINTEGERNOT NULL
started_atTIMESTAMPNOT NULL
watch_secondsINTEGERNOT NULL

Sample data:

content_idevent_idstarted_atuser_idwatch_seconds
10112025-01-01 08:15:0011800
10222025-01-01 09:00:0011200
10132025-01-01 20:20:0022400
10342025-01-01 12:00:0033600
10152025-01-02 07:30:0011500
10462025-01-02 19:45:002600
10572025-01-02 21:10:002900
10682025-01-02 10:05:0042700
10792025-01-02 14:30:0051200

Expected output

Two-day cumulative watch-time per user for today = 2025-01-02, summing 2025-01-02 and 2025-01-01 and including users present on only one day via FULL OUTER JOIN.

two_day_watch_secondsuser_id
45001
39002
36003
27004
12005

Hints

  1. First aggregate to per-user, per-day totals from watch_events.
  2. Parameterize the target date (today) and define yesterday as today - INTERVAL '1 day'.
  3. Use a FULL OUTER JOIN between today and yesterday user-level totals and COALESCE nulls to zero.