Description
Table: Customers
+-------------+---------+
| Column Name | Type |
+-------------+---------+
| id | int |
| name | varchar |
+-------------+---------+
id is the primary key (column with unique values) for this table.
Each row of this table indicates the ID and name of a customer.
Table: Orders
+-------------+------+
| Column Name | Type |
+-------------+------+
| id | int |
| customerId | int |
+-------------+------+
id is the primary key (column with unique values) for this table.
customerId is a foreign key (reference columns) of the ID from the Customers table.
Each row of this table indicates the ID of an order and the ID of the customer who ordered it.
Write a solution to find all customers who never order anything.
Return the result table in any order.
The result format is in the following example.
Examples:
Example 1:
Input:
Customers table:
+----+-------+
| id | name |
+----+-------+
| 1 | Joe |
| 2 | Henry |
| 3 | Sam |
| 4 | Max |
+----+-------+
Orders table:
+----+------------+
| id | customerId |
+----+------------+
| 1 | 3 |
| 2 | 1 |
+----+------------+
Output:
+-----------+
| Customers |
+-----------+
| Henry |
| Max |
+-----------+
Solution in Pandas
To solve this problem using Pandas, we need to identify customers who do not have any corresponding entries in the Orders
table.
- Merge the
Customers
andOrders
DataFrames:- Perform a left join on the
Customers
DataFrame with theOrders
DataFrame on theid
column ofCustomers
andcustomerId
column ofOrders
.
- Perform a left join on the
- Filter out customers with orders:
- Identify rows where the
customerId
column in the merged DataFrame isNaN
(indicating that the customer has no orders).
- Identify rows where the
- Select and format the output:
- Extract the names of customers who meet the condition.
Python
import pandas as pd
def find_customers(customers: pd.DataFrame, orders: pd.DataFrame) -> pd.DataFrame:
# Perform a left join on the 'Customers' and 'Orders' DataFrames
merged_df = customers.merge(orders, left_on='id', right_on='customerId', how='left')
# Filter out customers who have no orders (customerId is NaN in the merged DataFrame)
no_orders_df = merged_df[merged_df['customerId'].isna()]
# Select the 'name' column and rename it to 'Customers'
result_df = no_orders_df[['name']].rename(columns={'name': 'Customers'})
return result_df
Explanation of the code:
- Merging DataFrames:
customers.merge(orders, left_on='id', right_on='customerId', how='left')
: This performs a left join between theCustomers
andOrders
DataFrames. Theleft_on='id'
andright_on='customerId'
ensure that we join on the customer IDs. Thehow='left'
parameter ensures that all rows from theCustomers
DataFrame are included, along with matching rows from theOrders
DataFrame (if any).
- Filtering for customers without orders:
merged_df[merged_df['customerId'].isna()]
: This filters the merged DataFrame to include only rows where thecustomerId
from theOrders
table isNaN
, indicating that the customer has no orders.
- Selecting and Renaming Columns:
no_orders_df[['name']].rename(columns={'name': 'Customers'})
: This selects thename
column from the filtered DataFrame and renames it toCustomers
to match the required output format.
Solution in MySQL
SQL
SELECT c.name AS Customers
FROM Customers c
LEFT JOIN Orders o ON c.id = o.customerId
WHERE o.customerId IS NULL;
Solution in PostgreSQL
SQL
-- Select customers who do not have any orders
SELECT c.name AS Customers
FROM Customers c
LEFT JOIN Orders o ON c.id = o.customerId
-- Filter to include only customers with no corresponding orders
WHERE o.customerId IS NULL;