This one took me a while to even parse correctly.
Start by clarifying the data model and defining the 28-day window and active day. Then build two mutually exclusive cohorts using conditional aggregation to flag exclusive social or game users, excluding mixed and neither. Finally, compute the required metrics per cohort using CTEs for readability and handle nulls and non-positive durations with NULLIF and CASE.
Pro tip: Use a single pass over the session data with conditional aggregation to avoid multiple scans, and explicitly state how you handle edge cases like zero or negative durations and null user IDs, as this demonstrates production-ready SQL.
Confirm the definition of an active day (e.g., any session with positive duration), the 28-day window boundaries, and how to treat nulls and non-positive durations. State that you will exclude users with no sessions in either category.
Use a CTE to aggregate sessions per user, computing total active days (distinct dates with valid sessions) and total valid minutes (sum of durations where duration > 0). Filter out null user IDs and non-positive durations.
In another CTE, flag users who have only social sessions and users who have only game sessions. Exclude users who have both or neither by using conditional aggregation on category counts.
Join the cohort flags with the user-level aggregates, then compute user count, median active days (using PERCENTILE_CONT), percentage of users with 10+ active days, and average minutes per active day (total minutes / total active days, handling division by zero).
Present the final query with clear column aliases and cohort labels. Mention that you would validate by checking cohort sizes and ensuring no user appears in both cohorts.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.