<aside> 💡
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>
'confirmed' or 'timeout'.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.
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;
LEFT JOIN ensures all users from the Signups table are included, even if they have no confirmation records in the Confirmations table.SUM(CASE WHEN c.action = 'confirmed' THEN 1 ELSE 0 END) counts the number of confirmed actions for each user.COUNT(c.action) counts the total number of confirmation requests for each user.COALESCE(..., 0) handles cases where no confirmation requests exist, ensuring the confirmation rate is 0.ROUND(..., 2) rounds the confirmation rate to two decimal places.GROUP BY s.user_id ensures the calculation is done separately for each user.