The interview report preserved the survey tables and the request to compare response levels for new and old users, but it explicitly noted that the interviewer should clarify the cohort definition. The following is a self-contained practice reconstruction with that business rule supplied.
You have two PostgreSQL tables:
survey
------
user_id BIGINT NOT NULL
event_date DATE NOT NULL
survey_event TEXT NOT NULL -- 'impression' or 'response'
response INTEGER NULL -- 1 through 5 for a response; NULL for an impression
users
-----
user_id BIGINT PRIMARY KEY
reg_date DATE NOT NULL
country TEXT NOT NULL
Every survey.user_id has a matching users row. For this exercise:
-
A response from day 0 through day 29 after registration is from a
new
user.
-
A response on day 30 or later is from an
established
user.
-
Rows dated before
reg_date
are invalid and must be excluded.
-
Only rows with
survey_event = 'response'
and a non-null integer
response
from 1 through 5 are included.
-
Each qualifying row is one response event; a user may respond on multiple dates and each event counts.
Write one PostgreSQL query that returns, for each country and user cohort:
-
country
-
user_cohort
: exactly
new
or
established
-
response_events
: number of qualifying response events
-
responding_users
: number of distinct users with at least one qualifying response in the cohort
-
average_response
: the arithmetic mean of the qualifying 1-to-5 responses as a numeric value
Do not emit groups with zero qualifying responses. Preserve numeric precision and order by country ascending, then user_cohort ascending.