This looked like a clean aggregation problem until I started thinking about the denominator.
Start by clarifying the exact three-day window and the definition of 'only mobile', 'only desktop', and 'both' using set logic on distinct user IDs. Then write a single SQL query that aggregates distinct users per channel, computes the intersections and differences, and calculates counts and percentages with proper rounding. Finally, discuss corner cases such as users with multiple logins, timezone handling, and nulls.
Pro tip: Explicitly state your assumptions about the time window boundaries (inclusive/exclusive) and timezone, and show how you'd validate the query with edge cases like users appearing in both tables or having multiple logins—this demonstrates production-level rigor.
Confirm the exact three-day window (e.g., '2023-01-01' to '2023-01-03'), whether boundaries are inclusive, and the timezone. Define 'only mobile' as users in mobile_logins but not in desktop_logins, 'only desktop' as the reverse, and 'both' as the intersection.
Use subqueries or CTEs to get distinct user_ids from mobile_logins and desktop_logins within the window. This handles duplicate logins and ensures each user is counted once per channel.
Use INNER JOIN for 'both', LEFT JOIN with NULL check for 'only mobile', and RIGHT JOIN or equivalent for 'only desktop'. Alternatively, use set operators like INTERSECT and EXCEPT if supported.
Count users in each category and compute percentages relative to the total distinct users across both channels. Round percentages to two decimals using ROUND().
Handle NULL user_ids (exclude them), timezone conversions (use AT TIME ZONE if needed), and users with multiple logins (already handled by DISTINCT). Validate by manually checking a small sample or using a temporary table.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.