Per-user notification delivery summary from each notification's latest delivery attempt

Quick Overview

Write a SQL query that builds a per-user notification delivery summary with channel counts, delivered, permanently failed and still-retrying totals, and a delivery rate. It tests picking the latest row per group, conditional aggregation and rounding a percentage correctly.

Per-user notification delivery summary from each notification's latest delivery attempt

Company: Coinbase

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Other

A notification service stores every notification it sends and every delivery attempt made for it. A notification's current state is the status of its latest `delivery_logs` entry by `attempted_at`; earlier attempts do not affect the status columns. Produce a per-user delivery summary ordered by `user_id` ascending, with these columns: - `user_id`, `username` - `total_notifications`, plus `email_count`, `sms_count`, `push_count` (per channel) - `delivered_count`: latest status `delivered` - `permanently_failed`: latest status `failed` and `retry_count >= max_retries` - `pending_or_retrying`: latest status `pending`, or `failed` with retries remaining - `delivery_rate_pct`: delivered / total as a percentage, rounded to 2 decimals ```hint Shape the log first Reduce `delivery_logs` to exactly one row per notification before joining it to anything else, and check that your join cannot multiply a notification. ``` ### Input Table(s) `users` (primary key `user_id`) | column | type | meaning | |---|---|---| | user_id | INT | User identifier | | username | VARCHAR | User's display name | `notifications` (primary key `notification_id`; `user_id` references `users`) | column | type | meaning | |---|---|---| | notification_id | INT | Notification identifier | | user_id | INT | Recipient of the notification | | channel | VARCHAR | `'Email'`, `'SMS'` or `'Push'` | | max_retries | INT | Retries allowed before the notification is given up on | `delivery_logs` (one row per delivery attempt; `notification_id` references `notifications`; `(notification_id, attempted_at)` is unique) | column | type | meaning | |---|---|---| | notification_id | INT | Notification this attempt belongs to | | status | VARCHAR | `'pending'`, `'delivered'` or `'failed'` | | retry_count | INT | Retries made as of this attempt; may be NULL | | attempted_at | TIMESTAMP | When the attempt was made | ### Output Contract - Columns, in this order: `user_id`, `username`, `total_notifications`, `email_count`, `sms_count`, `push_count`, `delivered_count`, `permanently_failed`, `pending_or_retrying`, `delivery_rate_pct`. - One row per user who has at least one notification. Users with no notifications do not appear. - All count columns are integers. `delivery_rate_pct` is `delivered_count * 100 / total_notifications` computed in decimal arithmetic and rounded to 2 decimal places. - Rows are ordered by `user_id` ascending. ### Example `users` | user_id | username | |---|---| | 1 | alice | | 2 | bob | | 3 | carol | `notifications` | notification_id | user_id | channel | max_retries | |---|---|---|---| | 101 | 1 | Email | 3 | | 102 | 1 | SMS | 2 | | 103 | 1 | Push | 1 | | 104 | 1 | Email | 3 | | 105 | 2 | Email | 0 | | 106 | 2 | SMS | 2 | | 107 | 2 | Push | 3 | `delivery_logs` | notification_id | status | retry_count | attempted_at | |---|---|---|---| | 101 | pending | 0 | 2024-05-01 09:00:00 | | 101 | delivered | 1 | 2024-05-01 09:05:00 | | 102 | failed | 1 | 2024-05-01 10:00:00 | | 102 | failed | 2 | 2024-05-01 10:30:00 | | 103 | failed | 0 | 2024-05-01 11:00:00 | | 104 | pending | 0 | 2024-05-03 08:00:00 | | 105 | failed | 0 | 2024-05-02 08:00:00 | | 106 | failed | 0 | 2024-05-02 09:00:00 | | 106 | delivered | 1 | 2024-05-02 09:10:00 | | 107 | delivered | 0 | 2024-05-02 12:00:00 | Expected result: | user_id | username | total_notifications | email_count | sms_count | push_count | delivered_count | permanently_failed | pending_or_retrying | delivery_rate_pct | |---|---|---|---|---|---|---|---|---|---| | 1 | alice | 4 | 2 | 1 | 1 | 1 | 1 | 2 | 25.00 | | 2 | bob | 3 | 1 | 1 | 1 | 2 | 1 | 0 | 66.67 | For alice, 101 ends delivered, 102 ends failed with 2 of 2 retries used (permanent), 103 ends failed with 0 of 1 retries used (retrying) and 104 is pending. For bob, 105 failed with `max_retries = 0`, so one failure is permanent, and 106 and 107 end delivered. carol has no notifications and is left out. ### Constraints and Clarifications - `channel` is one of `'Email'`, `'SMS'`, `'Push'`. `status` is one of `'pending'`, `'delivered'`, `'failed'`. - `max_retries >= 0` and is never NULL. - Each notification has at least one delivery log entry. - No two log entries of the same notification share an `attempted_at`, so the latest entry is unique. - Treat a NULL `retry_count` as 0. - `1 <= user_id <= 10^4`, `1 <= notification_id <= 10^6`. - The three status columns split a user's notifications with no overlap, so `delivered_count + permanently_failed + pending_or_retrying = total_notifications` on every row.

Overview: Write a SQL query that builds a per-user notification delivery summary with channel counts, delivered, permanently failed and still-retrying totals, and a delivery rate. It tests picking the latest row per group, conditional aggregation and rounding a percentage correctly.

Coinbase logo
Coinbase
Nov 24, 2026
mediumData ScientistOtherData Manipulation (SQL/Python)
0
0

A notification service stores every notification it sends and every delivery attempt made for it. A notification's current state is the status of its latest delivery_logs entry by attempted_at; earlier attempts do not affect the status columns.

Produce a per-user delivery summary ordered by user_id ascending, with these columns:

  • user_id , username
  • total_notifications , plus email_count , sms_count , push_count (per channel)
  • delivered_count : latest status delivered
  • permanently_failed : latest status failed and retry_count >= max_retries
  • pending_or_retrying : latest status pending , or failed with retries remaining
  • delivery_rate_pct : delivered / total as a percentage, rounded to 2 decimals

Input Table(s)

users (primary key user_id)

columntypemeaning
user_idINTUser identifier
usernameVARCHARUser's display name

notifications (primary key notification_id; user_id references users)

columntypemeaning
notification_idINTNotification identifier
user_idINTRecipient of the notification
channelVARCHAR'Email', 'SMS' or 'Push'
max_retriesINTRetries allowed before the notification is given up on

delivery_logs (one row per delivery attempt; notification_id references notifications; (notification_id, attempted_at) is unique)

columntypemeaning
notification_idINTNotification this attempt belongs to
statusVARCHAR'pending', 'delivered' or 'failed'
retry_countINTRetries made as of this attempt; may be NULL
attempted_atTIMESTAMPWhen the attempt was made

Output Contract

  • Columns, in this order: user_id , username , total_notifications , email_count , sms_count , push_count , delivered_count , permanently_failed , pending_or_retrying , delivery_rate_pct .
  • One row per user who has at least one notification. Users with no notifications do not appear.
  • All count columns are integers. delivery_rate_pct is delivered_count * 100 / total_notifications computed in decimal arithmetic and rounded to 2 decimal places.
  • Rows are ordered by user_id ascending.

Example

users

user_idusername
1alice
2bob
3carol

notifications

notification_iduser_idchannelmax_retries
1011Email3
1021SMS2
1031Push1
1041Email3
1052Email0
1062SMS2
1072Push3

delivery_logs

notification_idstatusretry_countattempted_at
101pending02024-05-01 09:00:00
101delivered12024-05-01 09:05:00
102failed12024-05-01 10:00:00
102failed22024-05-01 10:30:00
103failed02024-05-01 11:00:00
104pending02024-05-03 08:00:00
105failed02024-05-02 08:00:00
106failed02024-05-02 09:00:00
106delivered12024-05-02 09:10:00
107delivered02024-05-02 12:00:00

Expected result:

user_idusernametotal_notificationsemail_countsms_countpush_countdelivered_countpermanently_failedpending_or_retryingdelivery_rate_pct
1alice421111225.00
2bob311121066.67

For alice, 101 ends delivered, 102 ends failed with 2 of 2 retries used (permanent), 103 ends failed with 0 of 1 retries used (retrying) and 104 is pending. For bob, 105 failed with max_retries = 0, so one failure is permanent, and 106 and 107 end delivered. carol has no notifications and is left out.

Constraints and Clarifications

  • channel is one of 'Email' , 'SMS' , 'Push' . status is one of 'pending' , 'delivered' , 'failed' .
  • max_retries >= 0 and is never NULL.
  • Each notification has at least one delivery log entry.
  • No two log entries of the same notification share an attempted_at , so the latest entry is unique.
  • Treat a NULL retry_count as 0.
  • 1 <= user_id <= 10^4 , 1 <= notification_id <= 10^6 .
  • The three status columns split a user's notifications with no overlap, so delivered_count + permanently_failed + pending_or_retrying = total_notifications on every row.