Query US-based users and most active forums
RedditData Scientist · Technical ScreenLast updated
This question evaluates SQL data manipulation skills, including time-based filtering, joins, aggregations, null-safe handling, ratio computation, distinct counts, and top-N ranking to determine user residency and forum activity.
You are given user and post data for an online forum platform.
Tables (schemas)
Assume the following tables:
users
user_idBIGINT (PK)home_country_codeVARCHAR (nullable; e.g., 'US', 'CA')created_atTIMESTAMP
posts
post_idBIGINT (PK)user_idBIGINT (FK →users.user_id)forum_idBIGINT (FK →forums.forum_id)created_atTIMESTAMPip_country_codeVARCHAR (nullable; country inferred from the posting IP)
forums
forum_idBIGINT (PK)forum_nameVARCHAR
Time assumptions
- Use the last 30 days of
posts.created_atrelative toCURRENT_DATE. - Treat timestamps as UTC.
Tasks
Write SQL queries for the following:
- Identify which users are “in the US.”
- Define a user as “in the US” if either:
users.home_country_code = 'US', or- among their posts in the last 30 days, at least 50% have
ip_country_code = 'US'. - Output columns:
user_idus_flag(1 if in US else 0)us_post_share_30d(US posts / total posts in last 30 days; null if no posts)
- Find the most active forums in the last 30 days.
- “Most active” should be measured by:
- primary metric:
post_count_30d - tie-breaker:
active_user_count_30d(distinct posters) - Output the top 10 forums with columns:
forum_idforum_namepost_count_30dactive_user_count_30d
Notes:
- Handle null country codes safely.
- You may use CTEs and window functions.
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.