This question evaluates SQL data manipulation competency, focusing on aggregation filtering with HAVING and the use of window functions for running counts and ranking within a fraud analytics scenario; Category: Data Manipulation (SQL/Python).
You work on fraud analytics. Assume the following schema (PostgreSQL-like types):
transactionstxn_id
BIGINT (PK)
merchant_id
BIGINT
user_id
BIGINT
region
TEXT
amount_usd
NUMERIC(12,2)
created_at
TIMESTAMP (UTC)
is_fraud
BOOLEAN
HAVING)For the last 30 days (relative to CURRENT_DATE, assume UTC), find merchants that:
#fraud_txns / #total_txns
).
Output columns: merchant_id, total_txns, fraud_txns, fraud_rate.
For each user_id, compute for every transaction:
Output columns: txn_id, user_id, created_at, txns_last_7d, recency_rank.