39. Inactive Customers
You are managing an e-commerce platform and want to identify customers who have never placed an order. This can help your marketing or sales team target those users with special promotions or reminders.
You are given two tables:
Customers:
āāāāāāāāāāāāāāā¦āāāāāāāāāāā ā Column Name ā Type ā ā āāāāāāāāāāāāāā¬āāāāāāāāāā⣠ā id ā int ā āāāāāāāāāāāāāāā¼āāāāāāāāāāā ā name ā vachar ā āāāāāāāāāāāāāāā©āāāāāāāāāāā
- id: Unique id of the customer.
- name: name of each customer.
Orders:
āāāāāāāāāāāāāāā¦āāāāāāāāāāā ā Column Name ā Type ā ā āāāāāāāāāāāāāā¬āāāāāāāāāā⣠ā id ā int ā āāāāāāāāāāāāāāā¼āāāāāāāāāāā ā customerid ā int ā āāāāāāāāāāāāāāā©āāāāāāāāāāā
- id: Unique id of the order
- customerId: customerId is a foreign key, which is taking reference from Customer table
Write an SQL query to return the names of all customers who do not appear in the Orders table.
The result should be ordered by customer name in ascending order.
Example 1:
Example:
Input:
Customers Table:
āāāāāāāāāāāā¦āāāāāāāāāāā ā id ā name ā ā āāāāāāāāāāā¬āāāāāāāāāā⣠ā 1 ā Joe ā āāāāāāāāāāāā¼āāāāāāāāāāā ā 2 ā Henry ā āāāāāāāāāāāā¼āāāāāāāāāāā ā 3 ā Sam ā āāāāāāāāāāāā¼āāāāāāāāāāā ā 4 ā Max ā āāāāāāāāāāāā©āāāāāāāāāāā
Orders Table:
āāāāāāāāāāāā¦āāāāāāāāāāāāā ā id ā customerId ā ā āāāāāāāāāāā¬āāāāāāāāāāāā⣠ā 1 ā 3 ā āāāāāāāāāāāā¼āāāāāāāāāāāāā ā 2 ā 1 ā āāāāāāāāāāāā©āāāāāāāāāāāāā
Expected Output:
āāāāāāāāāāāāā ā Customers ā ā āāāāāāāāāāā⣠ā Henry ā āāāāāāāāāāāāā ā Max ā āāāāāāāāāāāāā
Explanation:
- Joe (1) = OrderedĀ
- Henry (2) = No orderĀ
- Sam (3) = OrderedĀ
- Max (4) = No orderĀ
Still unsure what the problem is asking ?
Letās go through a few more examples, step by step, to make it clearer.