Simple Solution using group by

  • 3
    select customer_number from orders
    group by customer_number
    order by count(*) desc limit 1;

  • 0

    @Wondergirl46 how would you handle the follow up question
    "What if more than one customer have the largest number of orders, can you find all the customer_number in this case?"

  • 2

    In that case, the query would be:

    SELECT customer_number FROM orders 
    GROUP BY customer_number 
    HAVING COUNT(*) = (SELECT MAX(counts) 
                       FROM (SELECT COUNT(*) as counts FROM orders
                             GROUP BY customer_number) as sub)

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.