Customer Who Visited but Did Not Make Any Transactions - Problem

Imagine you're a data analyst for a bustling shopping mall, and you need to identify customers who window shop without making any purchases!

You have two important datasets:

  • Visits Table: Records every time a customer enters the mall
  • Transactions Table: Records only the visits where customers made purchases

Your task is to find customers who visited the mall but never made any transactions during those visits, and count how many times each customer had these "non-purchasing" visits.

Goal: Return a list showing each customer's ID and their count of visits without transactions, helping the mall understand customer browsing patterns.

Example: If customer 123 visited 5 times but only made purchases during 2 visits, they should appear in your result with count = 3.

Input & Output

example_1.sql โ€” Basic Case
$ Input: Visits: [[1,23],[2,9],[4,30],[5,54],[6,96],[7,54],[8,54]] Transactions: [[2,5,310],[3,5,300],[9,5,200],[12,1,910],[13,2,970]]
โ€บ Output: [[54,2],[30,1],[96,1]]
๐Ÿ’ก Note: Customer 54 visited 3 times (visits 5,7,8) but made 0 transactions. Customer 30 visited 1 time (visit 4) with 0 transactions. Customer 96 visited 1 time (visit 6) with 0 transactions. Customer 23 and 9 made transactions so they're excluded.
example_2.sql โ€” All Customers Make Transactions
$ Input: Visits: [[1,23],[2,9]] Transactions: [[1,1,100],[2,2,200]]
โ€บ Output: []
๐Ÿ’ก Note: Every visit has a corresponding transaction, so no customers visited without making purchases.
example_3.sql โ€” No Transactions At All
$ Input: Visits: [[1,23],[2,9],[3,23]] Transactions: []
โ€บ Output: [[23,2],[9,1]]
๐Ÿ’ก Note: No transactions exist, so all visits are counted. Customer 23 visited twice, customer 9 visited once.

Visualization

Tap to expand
Mall Entrance๐Ÿ‘คCustomer 54Visit #1Store Checkout๐Ÿ’ณTransaction RecordsLEFT JOIN๐Ÿ”—Match visits withtransactionsAnalysis ResultWindow ShoppersCustomer 54: 2 visitsNo purchases madeBuyersCustomer 9: 1 visitMade purchase โœ“SQL Query LogicLEFT JOIN Visits v ON Transactions tWHERE t.transaction_id IS NULL
Understanding the Visualization
1
Collect Entry Logs
Gather all customer visit records from mall entrance
2
Match with Purchase Records
Use LEFT JOIN to match visits with actual purchase transactions
3
Identify Window Shoppers
Find visits with NULL transactions (no purchases made)
4
Count Browsing Sessions
Group by customer and count their non-purchasing visits
Key Takeaway
๐ŸŽฏ Key Insight: LEFT JOIN naturally handles the 'find what's missing' pattern - it shows all visits and marks missing transactions as NULL, making it easy to count non-purchasing visits per customer.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(V ร— T)

For each visit V, we scan through all transactions T to check for matches

n
2n
โœ“ Linear Growth
Space Complexity
O(1)

No additional space needed beyond result storage

n
2n
โœ“ Linear Space

Constraints

  • 1 โ‰ค visits.length โ‰ค 105
  • transactions.length โ‰ค 105
  • 1 โ‰ค visit_id, transaction_id โ‰ค 106
  • 1 โ‰ค customer_id โ‰ค 104
  • All visit_id and transaction_id are unique
Asked in
Amazon 45 Meta 32 Google 28 Microsoft 22
42.3K Views
High Frequency
~8 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen