Count Visits with No Transactions for Each Customer

Quick Overview

For each customer, count visits with no matching transactions and return only customers with at least one such visit.

Count Visits with No Transactions for Each Customer

Company: Amazon

Role: Software Engineer

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

### Problem Find each customer with at least one Visits row having no matching transaction, and report customer_id and count_no_trans, the number of such visits. A customer may also have other visits with transactions. Any result row order is allowed. ### Tables Table: `Visits` ```text +-------------+---------+ | Column Name | Type | +-------------+---------+ | visit_id | int | | customer_id | int | +-------------+---------+ visit_id is the column with unique values for this table. This table contains information about the customers who visited the mall. ``` Table: `Transactions` ```text +----------------+---------+ | Column Name | Type | +----------------+---------+ | transaction_id | int | | visit_id | int | | amount | int | +----------------+---------+ transaction_id is column with unique values for this table. This table contains information about the transactions made during the visit_id. ``` ### Example ```text Visits (visit_id,customer_id): (1,8), (2,8), (3,8), (4,9), (5,10) Transactions (transaction_id,visit_id,amount): (1,1,20), (2,1,30), (3,4,40) Output (customer_id,count_no_trans): (8,2), (10,1) ``` ### What a Strong Answer Covers - Count visits without any transaction, even if the customer has transactions on other visits. - Return only qualifying customers and preserve the requested columns. ### Follow-up Questions - How would the result change if only successful transactions counted? - How would you return zero for customers who transacted on every visit?

Overview: For each customer, count visits with no matching transactions and return only customers with at least one such visit.

Amazon logo
Amazon
easySoftware EngineerOtherData Manipulation (SQL/Python)
0
0

Problem

Find each customer with at least one Visits row having no matching transaction, and report customer_id and count_no_trans, the number of such visits. A customer may also have other visits with transactions. Any result row order is allowed.

Tables

Table: Visits

+-------------+---------+
| Column Name | Type    |
+-------------+---------+
| visit_id    | int     |
| customer_id | int     |
+-------------+---------+
visit_id is the column with unique values for this table.
This table contains information about the customers who visited the mall.

Table: Transactions

+----------------+---------+
| Column Name    | Type    |
+----------------+---------+
| transaction_id | int     |
| visit_id       | int     |
| amount         | int     |
+----------------+---------+
transaction_id is column with unique values for this table.
This table contains information about the transactions made during the visit_id.

Example

Visits (visit_id,customer_id): (1,8), (2,8), (3,8), (4,9), (5,10)
Transactions (transaction_id,visit_id,amount): (1,1,20), (2,1,30), (3,4,40)
Output (customer_id,count_no_trans): (8,2), (10,1)

What a Strong Answer Covers Guidance

  • Count visits without any transaction, even if the customer has transactions on other visits.
  • Return only qualifying customers and preserve the requested columns.

Follow-up Questions Guidance

  • How would the result change if only successful transactions counted?
  • How would you return zero for customers who transacted on every visit?