Given three tables Customers, Orders, and Products, write a SQL query to find the most frequently ordered product(s) for each customer.
Requirements:
- Find the product(s) that each customer has ordered most frequently
- Include both
product_idandproduct_namein the result - Only include customers who have placed at least one order
- If a customer has multiple products with the same highest frequency, include all of them
Table Schemas:
Customers table contains customer information with unique customer_id.
Orders table contains order details. Note that no customer will order the same product more than once in a single day.
Products table contains product information with unique product_id.
Table Schema
| Column Name | Type | Description |
|---|---|---|
customer_id
PK
|
int | Unique customer identifier |
name
|
varchar | Customer name |
| Column Name | Type | Description |
|---|---|---|
order_id
PK
|
int | Unique order identifier |
order_date
|
date | Date when order was placed |
customer_id
|
int | Foreign key to Customers table |
product_id
|
int | Foreign key to Products table |
| Column Name | Type | Description |
|---|---|---|
product_id
PK
|
int | Unique product identifier |
product_name
|
varchar | Name of the product |
price
|
int | Price of the product |
Input & Output
| customer_id | name |
|---|---|
| 1 | Alice |
| 2 | Bob |
| 3 | Tom |
| order_id | order_date | customer_id | product_id |
|---|---|---|---|
| 1 | 2020-07-31 | 1 | 1 |
| 2 | 2020-07-30 | 2 | 2 |
| 3 | 2020-07-31 | 1 | 2 |
| 4 | 2020-07-29 | 3 | 3 |
| 5 | 2020-07-28 | 3 | 3 |
| 6 | 2020-07-27 | 1 | 2 |
| 7 | 2020-07-26 | 2 | 1 |
| 8 | 2020-07-25 | 2 | 1 |
| product_id | product_name | price |
|---|---|---|
| 1 | keyboard | 120 |
| 2 | mouse | 80 |
| 3 | screen | 600 |
| customer_id | product_id | product_name |
|---|---|---|
| 1 | 2 | mouse |
| 2 | 1 | keyboard |
| 3 | 3 | screen |
Customer 1 ordered product 2 (mouse) twice and product 1 (keyboard) once, so mouse is most frequent. Customer 2 ordered product 1 (keyboard) twice and product 2 (mouse) once, so keyboard is most frequent. Customer 3 ordered product 3 (screen) twice, making it the most frequent.
| customer_id | name |
|---|---|
| 1 | Alice |
| order_id | order_date | customer_id | product_id |
|---|---|---|---|
| 1 | 2020-07-31 | 1 | 1 |
| 2 | 2020-07-30 | 1 | 2 |
| 3 | 2020-07-29 | 1 | 1 |
| 4 | 2020-07-28 | 1 | 2 |
| product_id | product_name | price |
|---|---|---|
| 1 | keyboard | 120 |
| 2 | mouse | 80 |
| customer_id | product_id | product_name |
|---|---|---|
| 1 | 1 | keyboard |
| 1 | 2 | mouse |
Customer 1 ordered both keyboard and mouse exactly 2 times each. Since both products have the same highest frequency, both are included in the result.
| customer_id | name |
|---|---|
| 1 | Alice |
| 2 | Bob |
| order_id | order_date | customer_id | product_id |
|---|---|---|---|
| 1 | 2020-07-31 | 1 | 1 |
| 2 | 2020-07-30 | 2 | 2 |
| product_id | product_name | price |
|---|---|---|
| 1 | keyboard | 120 |
| 2 | mouse | 80 |
| customer_id | product_id | product_name |
|---|---|---|
| 1 | 1 | keyboard |
| 2 | 2 | mouse |
Each customer ordered only one product once, so those single products are their most frequently ordered items.
Constraints
-
1 ≤ customer_id ≤ 1000 -
1 ≤ order_id ≤ 10000 -
1 ≤ product_id ≤ 1000 - No customer will order the same product more than once in a single day
- All customers in the result must have placed at least one order