SQL 50, decoded
0 of 50 solved/50

1581. Customer Who Visited but Did Not Make Any Transactions

SQL 50 · Basic Joins (day 2). Decode it with the hints, solve it on LeetCode, then compare with the reference solution in MySQLPostgreSQL.

EasySolve it on LeetCode ↗

Your work

Hints

Open one at a time. Try to finish with as few as you can.

1. What does one output row represent?
One customer.
2. Approach
LEFT JOIN Transactions ON visit_id, WHERE transaction_id IS NULL, GROUP BY customer_id, COUNT(*).
3. The trap
Join on visit_id, not customer_id.

Reference solution

Tested on MySQL 8.0 and PostgreSQL 16. Switch dialect at the top of the page.

Show the MySQLPostgreSQL solution
SELECT v.customer_id, COUNT(*) AS count_no_trans
FROM Visits v
LEFT JOIN Transactions t ON v.visit_id = t.visit_id
WHERE t.transaction_id IS NULL
GROUP BY v.customer_id;

Patterns used

Which join · Phrase finder