cswork / libraryOpen practice workspace

Coding questions · Meta

Retrieve Ad Metrics and Rates for Last 7 Days

This question evaluates proficiency in SQL-based data manipulation including joins, conditional aggregation, time-based filtering, and computation of advertising metrics such as impressions, clicks, CTR, and conversion rate.

ads

+--------+---------------+----------+
| ad_id  | advertiser_id | ad_type  |
+--------+---------------+----------+
| 101    | 1001          | image    |
| 102    | 1002          | video    |
| 103    | 1001          | carousel |
+--------+---------------+----------+

impressions_clicks

+----------+--------+----------+------------+
| event_id | ad_id  | is_click | event_time |
+----------+--------+----------+------------+
| 1        | 101    | 0        | 2023-07-01 |
| 2        | 101    | 1        | 2023-07-01 |
| 3        | 102    | 0        | 2023-07-01 |
| 4        | 103    | 1        | 2023-07-02 |
+----------+--------+----------+------------+

conversions

+---------+----------+--------+------------+
| conv_id | event_id | ad_id  | conv_time  |
+---------+----------+--------+------------+
| 900     | 2        | 101    | 2023-07-01 |
| 901     | 4        | 103    | 2023-07-02 |
| 902     | 2        | 101    | 2023-07-03 |
+---------+----------+--------+------------+
Scenario

Three tables store information about ads, their impressions/clicks, and downstream conversions.

Question

Write a SQL query that returns, for each ad and calendar date in the last 7 days, impressions, clicks, conversions, CTR (clicks/impressions), and conversion_rate (conversions/clicks). Order by date, ad_id.

Hints

Join impressions_clicks and conversions on event_id/ad_id, aggregate with conditional sums, handle division by zero.

Daily Ad Metrics with CTR and Conversion Rate

Three tables store information about ads, their impressions/clicks, and downstream conversions.

Write a SQL query that returns, for each ad and each calendar date from 2025-05-26 through 2025-06-01 (inclusive), the number of impressions, clicks, conversions, CTR (clicks/impressions), and conversion_rate (conversions/clicks). Conversions should be attributed to the date of the corresponding click event (join on impressions_clicks.event_id and ad_id), not the conv_time date. Return one row per ad per date, including dates with zero activity, and order the result by event_date and ad_id.

Table ads

columntypeconstraints
ad_idINTEGERPRIMARY KEY, NOT NULL
advertiser_idINTEGERNOT NULL
ad_typeVARCHAR(50)NOT NULL

Sample data:

ad_idad_typeadvertiser_id
101image1001
102video1002
103carousel1001

Table impressions_clicks

columntypeconstraints
event_idINTEGERPRIMARY KEY, NOT NULL
ad_idINTEGERNOT NULL, FOREIGN KEY -> ads.ad_id
is_clickINTEGERNOT NULL, CHECK (is_click IN (0,1))
event_timeDATENOT NULL

Sample data:

ad_idevent_idevent_timeis_click
10112023-07-010
10122023-07-011
10232023-07-010
10342023-07-021

Table conversions

columntypeconstraints
conv_idINTEGERPRIMARY KEY, NOT NULL
event_idINTEGERNOT NULL, FOREIGN KEY -> impressions_clicks.event_id
ad_idINTEGERNOT NULL, FOREIGN KEY -> ads.ad_id
conv_timeDATENOT NULL

Sample data:

ad_idconv_idconv_timeevent_id
1019002023-07-012
1039012023-07-024
1019022023-07-032

Expected output

One row per ad_id per date between 2025-05-26 and 2025-06-01 (inclusive). Impressions = count of events with is_click = 0 on that date. Clicks = count of events with is_click = 1 on that date. Conversions are attributed to the date of the click event (join conversions to impressions_clicks on event_id and ad_id and use impressions_clicks.event_time as the conversion date), not conv_time. CTR = clicks / impressions (0.0 when impressions = 0). conversion_rate = conversions / clicks (0.0 when clicks = 0). Using the provided sample_data, all impression and conversion events occur in 2023, so there are no events in the 2025-05-26 to 2025-06-01 window and all metrics are zero.

ad_idclicksconversion_rateconversionsctrevent_dateimpressions
10100002025-05-260
10200002025-05-260
10300002025-05-260
10100002025-05-270
10200002025-05-270
10300002025-05-270
10100002025-05-280
10200002025-05-280
10300002025-05-280
10100002025-05-290
10200002025-05-290
10300002025-05-290
10100002025-05-300
10200002025-05-300
10300002025-05-300
10100002025-05-310
10200002025-05-310
10300002025-05-310
10100002025-06-010
10200002025-06-010
10300002025-06-010

Hints

  1. Join impressions_clicks and conversions on event_id and ad_id to attribute conversions to clicks.
  2. Aggregate with conditional sums for impressions (is_click = 0) and clicks (is_click = 1).
  3. Generate a date calendar from 2025-05-26 to 2025-06-01 and cross join with ads to include dates with zero activity.
  4. Use COALESCE and NULLIF in CASE expressions to avoid division by zero when computing CTR and conversion_rate.