Skip to content
We areTOP 1%

Confirmation Rate

MediumLeetCodeSQL: CASE, String & Date Functions
Solve on LeetCode(opens in a new tab)
Track your progress
First attempt
Re-solve after a few days
Final pass before interviews
Hint and exampleShow

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

Sign in to track your progress

Your checkboxes, streak and activity heatmap are saved to your account and sync across devices. It is free.
Sign in with email

New here? Create an account