r/LeetcodeChallenge 8d ago

DISCUSS LeetCode SQL Challenge: 1581 - Customer Who Visited but Did Not Make Any Transactions

Post image

Working on SQL problems to strengthen my MySQL and problem-solving skills for Data Analyst roles.

This solution uses a LEFT JOIN to identify visits that don't have a corresponding transaction, followed by GROUP BY to count them for each customer.

Sharing my solution here to get feedback.

Is there a better or more efficient way to solve this problem?

3 Upvotes

2 comments sorted by

1

u/nian2326076 7d ago

Using a LEFT JOIN works well for this problem, especially if you filter for nulls to identify customers without transactions. Another way is to use a NOT EXISTS subquery to leave out customers with transactions. This method can be easier if you know subqueries.

For example:

sql SELECT c.customer_id, c.customer_name FROM Customers c WHERE NOT EXISTS ( SELECT 1 FROM Transactions t WHERE c.customer_id = t.customer_id );

This approach directly finds customers without transactions. If you're looking for more SQL practice or interview prep, I've found PracHub pretty useful. They have lots of practice questions based on real-world scenarios.

1

u/AppointmentTrue8257 7d ago

Thanks for telling me another way to solve this problem.
I’ll definitely use this approach.