Customer Who Visited but Did Not Make Any Transactions
EasyLeetCodeSQL: Joins
Hint and exampleShowHide
Find rows without a match by using LEFT JOIN and keeping those where the right side IS NULL, then count them per group.
Example: logins(login_id, user_name) and purchases(purchase_id, login_id)
logins purchases
login_id | user_name purchase_id | login_id
1 | Asha 10 | 1
2 | Asha
3 | Ravi
4 | Asha
SELECT l.user_name, COUNT(*) AS empty_logins
FROM logins l
LEFT JOIN purchases p ON p.login_id = l.login_id
WHERE p.purchase_id IS NULL
GROUP BY l.user_name; -- Asha | 2, Ravi | 1