Sign in with Google

Google will share your name, email, and profile picture with DevExCode. See our privacy policy.

LeetCode 1581: Customer Who Visited but Did Not Make Any Transactions

Database

LeetCode 1581 Solution Explanation

Explanation:

To solve this problem, we need to identify customers who visited the mall but did not make any transactions. We can achieve this by performing a left join between the Visits and Transactions tables and then filtering out the rows where there is no corresponding transaction.

  1. Perform a left join between the Visits and Transactions tables on the visit_id column.
  2. Filter out the rows where there is no corresponding transaction (i.e., where transaction_id is NULL).
  3. Count the number of such occurrences for each customer_id.
  4. Return the customer_id and the count of visits without transactions.

Time complexity: O(n) Space complexity: O(n)

:

LeetCode 1581 Solutions in Java, C++, Python

# Write your Java solution here
SELECT v.customer_id AS customer_id, 
       COUNT(t.visit_id) AS count_no_trans
FROM Visits v
LEFT JOIN Transactions t
ON v.visit_id = t.visit_id
WHERE t.visit_id IS NULL
GROUP BY v.customer_id;

Interactive Code Editor for LeetCode 1581

Improve Your LeetCode 1581 Solution

Use the editor below to refine the provided solution for LeetCode 1581. Select a programming language and try the following:

  • Add import statements if required.
  • Optimize the code for better time or space complexity.
  • Add test cases to validate edge cases and common scenarios.
  • Handle error conditions or invalid inputs gracefully.
  • Experiment with alternative approaches to deepen your understanding.

Click "Run Code" to execute your solution and view the output. If errors occur, check the line numbers and debug accordingly. Resize the editor by dragging its bottom edge.

Loading editor...

Related LeetCode Problems