Google YouTube tracks every watch session in a table called watch_sessions:
watch_sessions (
session_id BIGINT PRIMARY KEY,
user_id BIGINT,
video_id BIGINT,
cohort_month VARCHAR(7), -- e.g., '2024-01' (user signup month)
platform VARCHAR(20), -- 'web', 'mobile', 'tablet', 'tv'
watch_duration_seconds INT,
session_start_date DATE
)
Write a SQL query that computes engagement metrics by user cohort and platform for sessions in the last 7 days:
- total_sessions: COUNT(DISTINCT session_id) per cohort and platform
- total_watch_seconds: SUM(watch_duration_seconds)
- completion_rate: % of sessions with watch_duration_seconds > 0
Output: cohort_month, platform, total_sessions, total_watch_seconds, completion_rate
Expected Output:
| Column | Type |
|---|
| cohort_month | TEXT (YYYY-MM format) |
| platform | TEXT |
| total_sessions | INTEGER |
| total_watch_seconds | INTEGER |
| completion_rate | REAL (0.0 to 100.0, 2 decimal places) |
Order by cohort_month ASC, platform ASC.
Use 0.0 when there are no sessions for a cohort-platform pair.