LeetCode MySQL-586. Customer Placing the Largest Number of Orders

    LeetCode MySQL

    Write an SQL query to find the customer_number for the customer who has placed the largest number of orders.

    寫一個 SQL Query來尋找下單訂單數量最多的客戶。
    

    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.
    

    Example 1:

    Input: 
    Orders table:
    +--------------+-----------------+
    | order_number | customer_number |
    +--------------+-----------------+
    | 1            | 1               |
    | 2            | 2               |
    | 3            | 3               |
    | 4            | 3               |
    +--------------+-----------------+
    Output: 
    +-----------------+
    | customer_number |
    +-----------------+
    | 3               |
    +-----------------+
    Explanation: 
    The customer with number 3 has two orders, which is greater than either customer 1 or 2 because each of them only has one order. 
    So the result is customer_number 3.
    

    Solution:
    1. 選擇標題 customer_number。
    2. 來自 Orders 的 table。
    3. 以 customer_number 為群組。
    4. 以 customer_number 的次數做降冪排序。
    5. 取該排列的第一個順位。

    Code1:

    SELECT 
    	customer_number
    	FROM Orders
    GROUP BY customer_number
    ORDER BY COUNT(customer_number) DESC
    LIMIT 1 OFFSET 0;
    

    newOrderss