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.