15. Visitors Without Transactions

A shopping mall management system tracks customer visits and transactions. Some customers visit the mall but do not make any purchases during their visit.

The mall’s management wants to identify these customers and determine how many times they visited without making a transaction.

Your task is to find all customers who visited without making any transactions and count the number of such visits per customer.

The company maintains two tables:

Visits table, which contains:

╔═════════════╦══════════╗
β•‘ Column Name β•‘   Type   β•‘
╠═════════════╬══════════╣
β•‘  visit_id   β•‘   int    β•‘
║─────────────┼──────────║
β•‘ customer_id β•‘   int    β•‘
β•šβ•β•β•β•β•β•β•β•β•β•β•β•β•β•©β•β•β•β•β•β•β•β•β•β•β•
  • visit_id: A unique identifier for each visit.
  • customer_id: The ID of the customer who visited the mall.

Transactions table, which contains:

╔════════════════╦══════════╗
β•‘  Column Name   β•‘   Type   β•‘
╠════════════════╬══════════╣
β•‘ transaction_id β•‘   int    β•‘
║────────────────┼──────────║
β•‘    visit_id    β•‘   int    β•‘
║────────────────┼──────────║
β•‘     amount     β•‘   int    β•‘
β•šβ•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•©β•β•β•β•β•β•β•β•β•β•β•
  • transaction_id: A unique identifier for each transaction.
  • visit_id: A reference to the visit in which the transaction occurred.
  • amount: The amount spent during the transaction.

Your task is to:

  • Find customers who visited but did not make any transactions.
  • Count how many times each customer made such visits.

Return the result in sorted by count_no_trans in descending order.

The sample output format is in the following example.

Example 1:

Example Input:

Visits Table

╔══════════╦═════════════╗
β•‘ visit_id β•‘ customer_id β•‘
╠══════════╬═════════════╣
β•‘    1     β•‘     23      β•‘
║──────────┼─────────────║
β•‘    2     β•‘      9      β•‘
║──────────┼─────────────║
β•‘    4     β•‘     30      β•‘
║──────────┼─────────────║
β•‘    5     β•‘     54      β•‘
║──────────┼─────────────║
β•‘    6     β•‘     96      β•‘
║──────────┼─────────────║
β•‘    7     β•‘     54      β•‘
║──────────┼─────────────║
β•‘    8     β•‘     54      β•‘
β•šβ•β•β•β•β•β•β•β•β•β•β•©β•β•β•β•β•β•β•β•β•β•β•β•β•β•

Transactions Table

╔════════════════╦══════════╦══════════╗
β•‘ transaction_id β•‘ visit_id β•‘  amount  β•‘
╠════════════════╬══════════╬══════════╣
β•‘       2        β•‘    5     β•‘   310    β•‘
║────────────────┼──────────┼──────────║
β•‘       3        β•‘    5     β•‘   300    β•‘
║────────────────┼──────────┼──────────║
β•‘       9        β•‘    5     β•‘   200    β•‘
║────────────────┼──────────┼──────────║
β•‘       12       β•‘    1     β•‘   910    β•‘
║────────────────┼──────────┼──────────║
β•‘       13       β•‘    2     β•‘   970    β•‘
β•šβ•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•©β•β•β•β•β•β•β•β•β•β•β•©β•β•β•β•β•β•β•β•β•β•β•

Output:

╔═════════════╦════════════════╗
β•‘ customer_id β•‘ count_no_trans β•‘
╠═════════════╬════════════════╣
β•‘     54      β•‘       2        β•‘
║─────────────┼────────────────║
β•‘     30      β•‘       1        β•‘
║─────────────┼────────────────║
β•‘     96      β•‘       1        β•‘
β•šβ•β•β•β•β•β•β•β•β•β•β•β•β•β•©β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•

Explanation:

  • Customer 23 - Made a transaction during visit 1, Excluded
  • Customer 9 - Made a transaction during visit 2, Excluded
  • Customer 30 - Visited once (visit 4) and did not make a transaction, Included
  • Customer 54 - Visited 3 times (visits 5, 7, 8) but made transactions only in visit 5 , Visited twice without a transaction , Included with count 2
  • Customer 96 - Visited once (visit 6) and did not make a transaction , Included

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Hints

0
Β 
Test Case

Input:

Visits
Transactions