cswork / libraryOpen practice workspace

Coding questions · Meta

Analyze Ad CTR and Convert Transactions to USD

This question evaluates proficiency in calculating and ranking per-user ad click-through rates and normalizing transaction amounts across currencies, testing competencies in aggregation, ranking logic, temporal joins, and currency conversion.

AdsImpressions

+-----------+---------+------------+--------+-----------+
| user_id   | ad_id   | impressions| clicks | event_dt  |
+-----------+---------+------------+--------+-----------+
| 101       | 55      | 12         | 3      | 2023-09-01|
| 102       | 77      | 7          | 0      | 2023-09-01|
| 101       | 77      | 5          | 2      | 2023-09-02|
| 103       | 55      | 9          | 1      | 2023-09-02|
| 104       | 88      | 11         | 4      | 2023-09-03|

ExchangeRates

+-----------+------------+-----------+
| currency  | rate_to_usd| rate_dt   |
+-----------+------------+-----------+
| USD       | 1.00       | 2023-09-01|
| EUR       | 1.10       | 2023-09-01|
| JPY       | 0.0068     | 2023-09-01|
| EUR       | 1.12       | 2023-09-02|
| JPY       | 0.0069     | 2023-09-02|

Transactions

+---------+---------+---------+----------+-----------+
| txn_id  | user_id | amount  | currency | txn_dt    |
+---------+---------+---------+----------+-----------+
| 5001    | 101     | 25.00   | EUR      | 2023-09-01|
| 5002    | 102     | 3000    | JPY      | 2023-09-01|
| 5003    | 101     | 10.00   | USD      | 2023-09-02|
| 5004    | 103     | 15.00   | EUR      | 2023-09-02|
| 5005    | 104     | 5000    | JPY      | 2023-09-03|
Scenario

E-commerce advertising analytics—query ad performance data and convert transaction amounts to USD.

Question

Write an SQL query that, for each user, returns the ad with the highest click-through-rate (CTR = clicks / impressions) over the last 7 days. Using the exchange_rates table, convert every transaction amount in transactions to its USD equivalent for the transaction date.

Hints

Consider window functions for CTR ranking and a date-based join for currency conversion.

Top CTR Ad Per User

Using the AdsImpressions table, compute click-through rate (CTR = clicks / impressions) aggregated per user and ad over the fixed 7-day window from DATE '2023-09-01' to DATE '2023-09-07' (inclusive). Return, for each user, the ad with the highest CTR; break ties by the smallest ad_id. Output columns: user_id, top_ad_id, ctr rounded to 4 decimals.

Table AdsImpressions

columntypeconstraints
user_idINTEGER
ad_idINTEGER
impressionsINTEGER
clicksINTEGER
event_dtDATE

Sample data:

ad_idclicksevent_dtimpressionsuser_id
5532023-09-0112101
7702023-09-017102
7722023-09-025101
5512023-09-029103
8842023-09-0311104

Expected output

One row per user with the ad that has the highest CTR during 2023-09-01 through 2023-09-07, ties broken by smallest ad_id.

ctrtop_ad_iduser_id
0.477101
077102
0.111155103
0.363688104

Hints

  1. Aggregate clicks and impressions per user and ad, then rank within each user by CTR.
  2. Cast to DECIMAL to avoid integer division; round CTR to 4 decimals.

Convert Transactions to USD

For each row in Transactions, convert amount to USD using ExchangeRates by choosing the most recent rate on or before txn_dt for the same currency. If currency is 'USD' and no rate exists, assume 1.0. Output txn_id, user_id, amount, currency, txn_dt, and amount_usd rounded to 2 decimals. Order by txn_id.

Table ExchangeRates

columntypeconstraints
currencyVARCHAR(3)
rate_to_usdDECIMAL(10,4)
rate_dtDATE

Sample data:

currencyrate_dtrate_to_usd
USD2023-09-011
EUR2023-09-011.1
JPY2023-09-010.0068
EUR2023-09-021.12
JPY2023-09-020.0069

Table Transactions

columntypeconstraints
txn_idINTEGER
user_idINTEGER
amountDECIMAL(12,2)
currencyVARCHAR(3)
txn_dtDATE

Sample data:

amountcurrencytxn_dttxn_iduser_id
25EUR2023-09-015001101
3000JPY2023-09-015002102
10USD2023-09-025003101
15EUR2023-09-025004103
5000JPY2023-09-035005104

Expected output

All transactions with amounts converted to USD using the latest exchange rate on or before the transaction date; USD defaults to 1.0 if missing.

amountamount_usdcurrencytxn_dttxn_iduser_id
2527.5EUR2023-09-015001101
300020.4JPY2023-09-015002102
1010USD2023-09-025003101
1516.8EUR2023-09-025004103
500034.5JPY2023-09-035005104

Hints

  1. Join on currency and rate_dt <= txn_dt, then pick the most recent rate using ROW_NUMBER.
  2. Use COALESCE to default USD to 1.0 when no rate is found and ROUND to 2 decimals.