Categories
Largest Number of Orders
Date
Author
A company maintains information about its orders in table called ORDERS. Write a query to find the CUSTOMER_ID of the customer who has placed the largest number of orders. If there is more than one customer with the same number of orders, then print the one with the smallest CUSTOMER_ID.
Input Format
ORDERS | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Name | Type | Description | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
ID | Integer | A number in the inclusive range [1, 1000] which uniquely identifies the order. This is the primary key. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
ORDER_DATE | Date | The date when the order was placed. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
STATUS | String | This is the order status. It can be PLACED, SHIPPED, IN TRANSIT, DELIVERED. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
CUSTOMER_ID | Integer | A number in the inclusive range [1, 1000] which uniquely identifies the customer who placed the order. |
Output Format
The output of the query should be the CUSTOMER_ID from the ORDERS table for the customer who has placed the largest number of orders. If there is more than one customer with the same number of orders, then print the one with the smallest CUSTOMER_ID.
ORDERS.CUSTOMER_ID
Sample Input
ORDERS | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
ID | ORDER_DATE | STATUS | CUSTOMER_ID | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
1 | 2003-01-06 | PLACED | 363 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
2 | 2003-01-06 | PLACED | 128 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
3 | 2003-01-06 | IN TRANSIT | 121 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
4 | 2003-01-06 | DELIVERED | 121 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
5 | 2003-03-06 | PLACED | 128 |
Sample Output
121
Explanation
The CUSTOMER_IDs 121 and 128 have placed the largest number of orders i.e two orders. The smaller CUSTOMER_ID is121.
Solution (MySQL):
SELECT CUSTOMER_ID
FROM ORDERS
GROUP BY CUSTOMER_ID
ORDER BY COUNT(*) DESC, CUSTOMER_ID ASC
LIMIT 1;