1. Description
Table: Orders
+-----------------+----------+
| Column Name | Type |
+-----------------+----------+
| order_number | int |
| customer_number | int |
+-----------------+----------+
order_number is the primary key for this table.
This table contains information about the order ID and the customer ID.
Write an SQL query to find the customer_number for the customer who has placed the largest number of orders. The test cases are generated so that exactly one customer will have placed more orders than any other customer.
2. Algorithms
- Group by customer_number.
- Find the customer_number with maximum number of orders.
3. Codes
/* Query1 */
# Write your MySQL query statement below
SELECT customer_number
FROM orders
GROUP BY customer_number
HAVING COUNT(*) = (SELECT MAX(num_orders)
FROM (SELECT customer_number, COUNT(*) AS num_orders
FROM orders
GROUP BY customer_number) AS order_max)
/* Query2 */
SELECT customer_number
FROM order
GROUP BY customer_number
ORDER BY COUNT(*) DESC
LIMIT 1;
4. Conclusion
'LeetCode > Easy' 카테고리의 다른 글
590 N-ary Tree Postorder Traversal (0) | 2022.09.07 |
---|---|
589 N-ary Tree Preorder Traversal (0) | 2022.09.07 |
584 Find Customer Referee (0) | 2022.09.07 |
575 Distribute Candies (0) | 2022.09.07 |
572 Subtree of Another Tree (0) | 2022.09.06 |