Confirmation Rate
MediumLeetCodeSQL: CASE, String & Date Functions
Hint and exampleShowHide
Keep every person with LEFT JOIN, then take AVG of a CASE that gives 1 for the wanted outcome and 0 otherwise, so people without any rows get a rate of 0.
Example: users(id) holds 1, 2 and 3; attempts(user_id, result)
user_id | result
1 | pass
1 | fail
2 | fail
SELECT u.id,
ROUND(AVG(CASE WHEN a.result = 'pass' THEN 1 ELSE 0 END), 2) AS pass_rate
FROM users u
LEFT JOIN attempts a ON a.user_id = u.id
GROUP BY u.id;
-- 1 | 0.50, 2 | 0.00, 3 | 0.00 (no attempts)
More in SQL: CASE, String & Date Functions
| # | Problem | Platform | Difficulty | Open | 1st1st Visit | 2nd2nd Visit | RevRevise |
|---|---|---|---|---|---|---|---|
| 1 | Triangle Judgement | LeetCode | Easy | Solve Triangle Judgement on LeetCode (opens in a new tab) | |||
| 2 | Fix Names in a Table | LeetCode | Easy | Solve Fix Names in a Table on LeetCode (opens in a new tab) | |||
| 3 | Rising Temperature | LeetCode | Easy | Solve Rising Temperature on LeetCode (opens in a new tab) | |||
| 4 | Group Sold Products By The Date | LeetCode | Easy | Solve Group Sold Products By The Date on LeetCode (opens in a new tab) | |||
| 5 | Monthly Transactions I | LeetCode | Medium | Solve Monthly Transactions I on LeetCode (opens in a new tab) | |||
| 6 | Confirmation Rate | LeetCode | Medium | Solve Confirmation Rate on LeetCode (opens in a new tab) |