Interview Questions/SQL/Customers Who Never Order... Wait - Customers With No Transactions on Visits

Customers Who Never Order... Wait - Customers With No Transactions on Visits

Preview mode. Log in to edit, run, submit, and save progress.

Medium

Description

You are given two tables - Visits and Transactions. The Visits table logs every time a customer visited a mall. The Transactions table logs purchases made during a visit. Not every visit results in a transaction. Write a SQL query to find the customer IDs of customers who visited the mall but made NO transactions, and count how many such visits each customer had. Return only customers who have at least one visit with no transaction. Return the result in any order.

Database Schema

Visits

Column NameExample Value
visit_id1
customer_id23

Transactions

Column NameExample Value
transaction_id2
visit_id5
amount310

Example

Visits

visit_idcustomer_id
123
29
430
554
696
754
854

Transactions

transaction_idvisit_idamount
25310
35300
95200
121910
132970

Output

customer_idcount_no_trans
542
301
961

Explanation:

Customer 23 and 9 made transactions on every visit. Customer 30 and 96 each had 1 visit with no transaction. Customer 54 had 3 visits - only 1 had transactions (visit 5), the other 2 (visits 7 and 8) had none.

Approach hint

Start with a simple approach, explain the trade-off, then move toward a cleaner or more scalable solution.

Common mistake

Skipping assumptions, edge cases, or trade-offs can make an otherwise good answer feel incomplete.

SQL Editor
Loading...

Visits

visit_idcustomer_id
123
29
430
554
696
754
854

Transactions

transaction_idvisit_idamount
25310
35300
95200
121910
132970

Output

customer_idcount_no_trans
542
301
961