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_id and time_stamp
  • Confirmations: Contains confirmation message attempts with user_id, time_stamp, and action (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
πŸ‘₯ SignupsUser 2User 3User 6User 7πŸ“§ ConfirmationsUser 2: βœ…confirmedUser 2: ❌timeoutUser 3: ❌timeoutUser 7: βœ…confirmedπŸ”— LEFT JOINGROUP BY user_idπŸ“Š ResultsUser 2: 0.50 (1/2)User 3: 0.00 (0/2)User 6: 0.00 (0/0)User 7: 1.00 (3/3)πŸ”§ SQL Query Breakdown:SELECT s.user_id, ROUND(COALESCE(SUM(CASE WHEN c.action = 'confirmed' THEN 1 ELSE 0 END) * 1.0/ NULLIF(COUNT(c.action), 0), 0), 2) as confirmation_rateFROM Signups s LEFT JOIN Confirmations c ON s.user_id = c.user_idGROUP BY s.user_id ORDER BY s.user_id;
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
Asked in
Meta 45 Amazon 38 Google 32 Microsoft 28
28.4K Views
Medium Frequency
~15 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
πŸ’‘ Explanation
AI Ready
πŸ’‘ Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen