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.
Assume the following tables:
usersuser_id
BIGINT (PK)
home_country_code
VARCHAR (nullable; e.g., 'US', 'CA')
created_at
TIMESTAMP
postspost_id
BIGINT (PK)
user_id
BIGINT (FK →
users.user_id
)
forum_id
BIGINT (FK →
forums.forum_id
)
created_at
TIMESTAMP
ip_country_code
VARCHAR (nullable; country inferred from the posting IP)
forumsforum_id
BIGINT (PK)
forum_name
VARCHAR
posts.created_at
relative to
CURRENT_DATE
.
Write SQL queries for the following:
users.home_country_code = 'US'
,
or
ip_country_code = 'US'
.
user_id
us_flag
(1 if in US else 0)
us_post_share_30d
(US posts / total posts in last 30 days; null if no posts)
post_count_30d
active_user_count_30d
(distinct posters)
forum_id
forum_name
post_count_30d
active_user_count_30d
Notes: