inblog logo
|
stupefyee
    SQL문제풀기

    [SQL 문제 풀이] Customer Placing the Largest Number of Orders (가장 많은 주문을 하는 고객)

    Stupefyee's avatar
    Stupefyee
    Jun 04, 2025
    [SQL 문제 풀이] Customer Placing the Largest Number of Orders (가장 많은 주문을 하는 고객)
    Contents
    내가 작성한 쿼리
    Customer Placing the Largest Number of Orders - LeetCode
    Can you solve this real interview question? Customer Placing the Largest Number of Orders - Table: Orders +-----------------+----------+ | Column Name | Type | +-----------------+----------+ | order_number | int | | customer_number | int | +-----------------+----------+ order_number is the primary key (column with unique values) for this table. This table contains information about the order ID and the customer ID.   Write a solution 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. The result format is in the following example.   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.   Follow up: What if more than one customer has the largest number of orders, can you find all the customer_number in this case?
    Customer Placing the Largest Number of Orders - LeetCode
    https://leetcode.com/problems/customer-placing-the-largest-number-of-orders/description/
    Customer Placing the Largest Number of Orders - LeetCode
    notion image
    가장 많은 주문을 한 고객을 위해 customer_number를 찾기 위한 솔루션을 작성하세요. 테스트 케이스는 한 고객이 다른 고객보다 더 많은 주문을 할 수 있도록 생성됩니다.
     

    내가 작성한 쿼리

    Oracle

    SELECT customer_number FROM ( SELECT customer_number, COUNT(*) AS count FROM Orders GROUP BY customer_number ORDER BY count DESC ) WHERE ROWNUM = 1;

    MySQL

    SELECT customer_number FROM orders GROUP BY customer_number ORDER BY COUNT(*) DESC LIMIT 1;

    차이점

    • 상위 결과값 1개를 출력하는 방식의 차
    Share article
    Contents
    내가 작성한 쿼리

    stupefyee

    RSS·Powered by Inblog