+-------------+---------+
|Column Name |Type|+-------------+---------+
| id | int || name | varchar |+-------------+---------+
id is the primarykeycolumnfor this table.
Eachrowof this table indicates the ID and name of a customer.
Table: Orders
1
2
3
4
5
6
7
8
9
+-------------+------+
|Column Name |Type|+-------------+------+
| id | int || customerId | int |+-------------+------+
id is the primarykeycolumnfor this table.
customerId is a foreignkeyof the ID from the Customers table.
Eachrowof this table indicates the ID of an orderand the ID of the customer who ordered it.
Write an SQL query to report all customers who never order anything.
SELECTc.name AS Customers FROM customers cWHEREc.id NOTIN (SELECT o.customerId FROM Orders o)
1
2
3
4
5
6
7
8
9
10
import pandas as pd
deffind_customers(customers: pd.DataFrame, orders: pd.DataFrame) -> pd.DataFrame:
# Select the customers whose 'id' is not present in the orders DataFrame's 'customerId' column. df = customers[~customers['id'].isin(orders['customerId'])]
# Build a DataFrame that only contains the 'name' column and rename it as 'Customers'. df = df[['name']].rename(columns={'name': 'Customers'})
return df
SELECTc.name AS Customers FROM customers cLEFTJOIN Orders o ONc.id = o.customerId
WHERE o.customerId ISNULL
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import pandas as pd
deffind_customers(customers: pd.DataFrame, orders: pd.DataFrame) -> pd.DataFrame:
# Merge the customers DataFrame with the orders DataFrame using a left join on 'id' and 'customerId' merged_df = customers.merge(orders, how='left', left_on='id', right_on='customerId')
# Use the 'customerId' column to create a boolean mask for customers who never placed any orders mask = merged_df['customerId'].isna()
# Filter the rows using the boolean mask result_df = merged_df[mask]
# Select only the 'name' column from the result DataFrame and rename it as 'Customers' result_df = result_df[['name']].rename(columns={'name': 'Customers'})
return result_df