Customer Placing the Largest Number of Orders - Problem
Customer Placing the Largest Number of Orders

You're working as a data analyst for an e-commerce platform and need to identify your most valuable customer based on order frequency. Given a database table containing order information, your task is to find the customer who has placed the most orders.

Table: Orders
Column NameType
order_numberint
customer_numberint

order_number is the primary key (unique identifier for each order)
customer_number identifies which customer placed the order
• The test cases guarantee that exactly one customer will have more orders than any other

Goal: Write a SQL query to return the customer_number of the customer with the highest number of orders.

Input & Output

example_1.sql — Basic Case
$ Input: Orders table: | order_number | customer_number | |--------------|----------------| | 1 | 101 | | 2 | 102 | | 3 | 103 | | 4 | 101 | | 5 | 101 |
Output: | customer_number | |----------------| | 101 |
💡 Note: Customer 101 has 3 orders (1, 4, 5), customer 102 has 1 order (2), and customer 103 has 1 order (3). Customer 101 has the most orders.
example_2.sql — Close Competition
$ Input: Orders table: | order_number | customer_number | |--------------|----------------| | 10 | 201 | | 11 | 202 | | 12 | 201 | | 13 | 202 | | 14 | 201 |
Output: | customer_number | |----------------| | 201 |
💡 Note: Customer 201 has 3 orders (10, 12, 14) while customer 202 has 2 orders (11, 13). Customer 201 wins by one order.
example_3.sql — Single Customer
$ Input: Orders table: | order_number | customer_number | |--------------|----------------| | 1 | 999 | | 2 | 999 | | 3 | 999 |
Output: | customer_number | |----------------| | 999 |
💡 Note: Only one customer (999) exists with 3 orders, so they are automatically the customer with the most orders.

Visualization

Tap to expand
Customer Order Analysis FlowRaw Ordersorder | customer1 | 1012 | 102GROUP BYcustomer_number101: [1,4,5]102: [2,3]COUNT(*)per group101: 3102: 2ORDER BYCOUNT DESC101: 3 ←102: 2LIMIT 1Take First101Complete SQL SolutionSELECTcustomer_numberFROMOrdersGROUP BYcustomer_number← Organize orders by customerORDER BYCOUNT(*) DESC← Sort by order count (highest first)LIMIT1;← Return only the top result✓ Single efficient query solution - O(n log k) where k = unique customers
Understanding the Visualization
1
Raw Data
Orders table with order_number and customer_number columns
2
Group By Customer
GROUP BY customer_number creates groups of orders for each customer
3
Count Orders
COUNT(*) calculates how many orders each customer has placed
4
Sort Descending
ORDER BY COUNT(*) DESC puts the customer with most orders first
5
Take Top Result
LIMIT 1 returns only the customer with the highest count
Key Takeaway
🎯 Key Insight: SQL's GROUP BY + ORDER BY + LIMIT pattern efficiently aggregates data and finds the maximum in one optimized database operation, much faster than manual counting approaches.

Time & Space Complexity

Time Complexity
⏱️
O(n log n)

GROUP BY is O(n), ORDER BY is O(k log k) where k is number of unique customers

n
2n
Linearithmic
Space Complexity
O(k)

Where k is the number of unique customers for grouping results

n
2n
Linear Space

Constraints

  • 1 ≤ order_number ≤ 106
  • 1 ≤ customer_number ≤ 106
  • Each order_number is unique (primary key)
  • Exactly one customer will have the maximum number of orders
Asked in
Amazon 45 Google 38 Microsoft 32 Meta 28 Apple 22
42.3K Views
High Frequency
~8 min Avg. Time
1.9K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen