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.

Hints

0
Ā 
Test Case

Input:

Customers
Orders