This question evaluates proficiency in SQL data manipulation for a Data Analyst role, focusing on time-window aggregations, conditional and group aggregations, and event-level metric computation; it is categorized under Data Manipulation (SQL/Python) and emphasizes practical application rather than purely conceptual understanding.
You are given three tables. Assume all timestamps are stored in UTC.
users(user_id BIGINT PRIMARY KEY, create_date TIMESTAMP)
: one row per user account.
transactions(transaction_id BIGINT PRIMARY KEY, user_id BIGINT, product VARCHAR, amount_gbp DECIMAL(12,2), status VARCHAR, transaction_time TIMESTAMP)
: one row per transaction.
status
is either
'completed'
or
'declined'
.
activity(user_id BIGINT, product VARCHAR, event_type VARCHAR, event_time TIMESTAMP)
: one row per product interaction.
event_type
is either
'view'
or
'click'
.
Write SQL for both tasks below:
[create_date, create_date + INTERVAL '7 day')
.
transactions
where
product = 'crypto'
and
status = 'completed'
.
user_id
,
total_crypto_amount_7d
.
activity
, where:
clicks = COUNT(*)
for
event_type = 'click'
views = COUNT(*)
for
event_type = 'view'
ctr = clicks / views
product
,
views
,
clicks
,
ctr
.