Confirmation Rate - Problem
You're building a user engagement analytics system for a mobile app that sends confirmation messages to users. Your task is to calculate the confirmation rate for each user based on their interaction history.
You have two tables:
- Signups: Contains user registration data with
user_idandtime_stamp - Confirmations: Contains confirmation message attempts with
user_id,time_stamp, andaction(either 'confirmed' or 'timeout')
The confirmation rate is calculated as: Number of 'confirmed' messages Γ· Total confirmation requests
Special Rules:
- Users who never requested confirmations have a rate of
0.00 - Round the result to 2 decimal places
- Include all users from the Signups table in your results
Input & Output
basic_case.sql β Basic Example
$
Input:
Signups: [(3, '2020-03-21 10:16:13'), (7, '2020-01-04 13:57:59'), (2, '2020-07-29 23:09:44'), (6, '2020-12-09 10:39:37')]
Confirmations: [(3, '2021-01-06 03:30:46', 'timeout'), (3, '2021-07-14 14:00:00', 'timeout'), (7, '2021-06-12 11:57:29', 'confirmed'), (7, '2021-06-13 12:58:28', 'confirmed'), (7, '2021-06-14 13:59:27', 'confirmed'), (2, '2021-01-22 00:00:00', 'confirmed'), (2, '2021-02-28 23:59:59', 'timeout')]
βΊ
Output:
[(2, 0.50), (3, 0.00), (6, 0.00), (7, 1.00)]
π‘ Note:
User 2: 1 confirmed out of 2 total = 0.50. User 3: 0 confirmed out of 2 total = 0.00. User 6: No confirmations = 0.00. User 7: 3 confirmed out of 3 total = 1.00.
single_user.sql β Single User
$
Input:
Signups: [(1, '2020-01-01 10:00:00')]
Confirmations: [(1, '2020-01-02 11:00:00', 'confirmed')]
βΊ
Output:
[(1, 1.00)]
π‘ Note:
User 1 has 1 confirmed message out of 1 total, resulting in a perfect confirmation rate of 1.00.
no_confirmations.sql β Edge Case
$
Input:
Signups: [(1, '2020-01-01 10:00:00'), (2, '2020-01-02 10:00:00')]
Confirmations: []
βΊ
Output:
[(1, 0.00), (2, 0.00)]
π‘ Note:
Both users signed up but never requested any confirmation messages, so their confirmation rates are 0.00.
Constraints
- 1 β€ signups.length β€ 104
- 0 β€ confirmations.length β€ 104
- Each user_id in Confirmations exists in Signups
- Confirmation rates must be rounded to exactly 2 decimal places
- Action values are only 'confirmed' or 'timeout'
Visualization
Tap to expand
Understanding the Visualization
1
LEFT JOIN Tables
Connect every signup with their confirmation attempts (if any)
2
Group By User
Aggregate all confirmation data for each user
3
Count & Calculate
Count confirmed vs total, compute rate with proper null handling
4
Format Result
Round to 2 decimal places and return user_id with rate
Key Takeaway
π― Key Insight: LEFT JOIN preserves all users while conditional aggregation efficiently calculates rates in a single query pass
π‘
Explanation
AI Ready
π‘ Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code