61. Order Count per Customer

In e-commerce analytics, identifying loyal and high-value customers is crucial. A customer consistently spending above a certain threshold in consecutive months might be eligible for loyalty rewards, marketing campaigns, or early product access.

You are given three tables:

Customers table:

╔═════════════╦══════════╗
ā•‘ Column Name ā•‘   Type   ā•‘
╠═════════════╬══════════╣
ā•‘ customer_id ā•‘   int    ā•‘
║─────────────┼──────────║
ā•‘    name     ā•‘ varchar  ā•‘
║─────────────┼──────────║
ā•‘   country   ā•‘ varchar  ā•‘
ā•šā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•©ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•
  • customer_id: Unique identifier for the customer (Primary Key)
  • name: Name of the customer
  • country: Country where the customer is located

Product table:

╔═════════════╦══════════╗
ā•‘ Column Name ā•‘   Type   ā•‘
╠═════════════╬══════════╣
ā•‘ product_id  ā•‘   int    ā•‘
║─────────────┼──────────║
ā•‘ description ā•‘ varchar  ā•‘
║─────────────┼──────────║
ā•‘    price    ā•‘   int    ā•‘
ā•šā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•©ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•
  • product_id: Unique identifier for the product (Primary Key)
  • description: Description of the product
  • price: Price of the product

Orders table:

╔═════════════╦══════════╗
ā•‘ Column Name ā•‘   Type   ā•‘
╠═════════════╬══════════╣
ā•‘   order_id  ā•‘   int    ā•‘
║─────────────┼──────────║
ā•‘ customer_id ā•‘   int    ā•‘
║─────────────┼──────────║
ā•‘ product_id  ā•‘   int    ā•‘
║─────────────┼──────────║
ā•‘ order_date  ā•‘   date   ā•‘
║─────────────┼──────────║
ā•‘  quantity   ā•‘   int    ā•‘
ā•šā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•©ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•
  • order_id: Unique order ID (Primary Key)
  • customer_id: References Customers.customer_id (who placed the order)
  • product_id: References Product.product_id (what was purchased)
  • order_date: Date the order was placed, date in format ("YYYY-MM-DD")
  • quantity: Number of units of the product purchased

Write a query to return the customer_id and customer_name of customers who have spent at least $100 in both June and July of 2020.

Example 1:

Example:

Input:

Customers Table:

╔═════════════╦══════════╦═════════╗
ā•‘ customer_id ā•‘   name   ā•‘ country ā•‘
╠═════════════╬══════════╬═════════╣
ā•‘      1      ā•‘ Winston  ā•‘   USA   ā•‘
║─────────────┼──────────┼─────────║
ā•‘      2      ā•‘ Jonathan ā•‘  Peru   ā•‘
║─────────────┼──────────┼─────────║
ā•‘      3      ā•‘ Moustafa ā•‘  Egypt  ā•‘
ā•šā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•©ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•©ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•

Product Table:

╔════════════╦═════════════╦═══════╗
ā•‘ product_id ā•‘ description ā•‘ price ā•‘
╠════════════╬═════════════╬═══════╣
ā•‘     10     ā•‘    Phone    ā•‘  300  ā•‘
║────────────┼─────────────┼───────║
ā•‘     20     ā•‘   T-Shirt   ā•‘  10   ā•‘
║────────────┼─────────────┼───────║
ā•‘     30     ā•‘    Book     ā•‘  45   ā•‘
║────────────┼─────────────┼───────║
ā•‘     40     ā•‘  Keychain   ā•‘   2   ā•‘
ā•šā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•©ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•©ā•ā•ā•ā•ā•ā•ā•ā•

Orders Table:

╔══════════╦═════════════╦════════════╦════════════╦══════════╗
ā•‘ order_id ā•‘ customer_id ā•‘ product_id ā•‘ order_date ā•‘ quantity ā•‘
╠══════════╬═════════════╬════════════╬════════════╬══════════╣
ā•‘    1     ā•‘      1      ā•‘     10     ā•‘ 2020-06-10 ā•‘    1     ā•‘
║──────────┼─────────────┼────────────┼────────────┼──────────║
ā•‘    2     ā•‘      1      ā•‘     20     ā•‘ 2020-07-01 ā•‘    1     ā•‘
║──────────┼─────────────┼────────────┼────────────┼──────────║
ā•‘    3     ā•‘      1      ā•‘     30     ā•‘ 2020-07-08 ā•‘    2     ā•‘
║──────────┼─────────────┼────────────┼────────────┼──────────║
ā•‘    4     ā•‘      2      ā•‘     10     ā•‘ 2020-06-15 ā•‘    2     ā•‘
║──────────┼─────────────┼────────────┼────────────┼──────────║
ā•‘    5     ā•‘      2      ā•‘     40     ā•‘ 2020-07-01 ā•‘   10     ā•‘
║──────────┼─────────────┼────────────┼────────────┼──────────║
ā•‘    6     ā•‘      3      ā•‘     20     ā•‘ 2020-06-24 ā•‘    2     ā•‘
║──────────┼─────────────┼────────────┼────────────┼──────────║
ā•‘    7     ā•‘      3      ā•‘     30     ā•‘ 2020-06-25 ā•‘    2     ā•‘
║──────────┼─────────────┼────────────┼────────────┼──────────║
ā•‘    9     ā•‘      3      ā•‘     30     ā•‘ 2020-05-08 ā•‘    3     ā•‘
ā•šā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•©ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•©ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•©ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•©ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•

Output:

╔═════════════╦═════════╗
ā•‘ customer_id ā•‘  name   ā•‘
╠═════════════╬═════════╣
ā•‘      1      ā•‘ Winston ā•‘
ā•šā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•©ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•

Explanation:

Winston:

  • June: 1 * 300 = 300
  • July: 1 * 10 + 2 * 45 = 100 → qualifies.

Jonathan:

  • June: 2 * 300 = 600
  • July: 10 * 2 = 20 → excluded.

Moustafa:

  • June: 2 * 10 + 2 * 45 = 110
  • July: 0 → excluded.

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
Product
Orders