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
•
•
• The test cases guarantee that exactly one customer will have more orders than any other
Goal: Write a SQL query to return the
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 Name | Type |
|---|---|
| order_number | int |
| customer_number | int |
•
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
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
⚡ Linearithmic
Space Complexity
O(k)
Where k is the number of unique customers for grouping results
✓ 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
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code