<aside> 💡

Project Overview:

This project calculates the confirmation rate of each user based on their confirmation requests. The confirmation rate is defined as the ratio of confirmed messages to total confirmation requests. The project demonstrates SQL techniques like joins, conditional aggregation, and handling cases where users have no confirmation requests.

</aside>


Problem Description:

Tables:

  1. Signups:
  2. Confirmations:

Each user's confirmation rate is calculated as: \[ \text{Confirmation Rate} = \frac{\text{Number of 'confirmed' actions}}{\text{Total confirmation requests}} \]

If a user has no confirmation requests, their confirmation rate is 0.


Solution:

The query calculates the confirmation rate using a LEFT JOIN to ensure all users are included, even if they have no confirmation requests. Conditional aggregation is used to count confirmed and total requests.

SELECT
    s.user_id,
    ROUND(
        COALESCE(SUM(CASE WHEN c.action = 'confirmed' THEN 1 ELSE 0 END) / COUNT(c.action), 0),
        2
    ) AS confirmation_rate
FROM
    Signups s
LEFT JOIN
    Confirmations c
ON
    s.user_id = c.user_id
GROUP BY
    s.user_id;


Explanation:

  1. Joining Tables:
  2. Counting Confirmations:
  3. Handling Missing Data:
  4. Rounding:
  5. Grouping: