Customer Placing the Largest Number of Orders - Problem

You are given a table Orders that contains information about orders and their corresponding customers.

Write a SQL 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.

Table Schema

Orders
Column Name Type Description
order_number PK int Primary key - unique identifier for each order
customer_number int Customer identifier who placed the order
Primary Key: order_number
Note: Each row represents one order placed by a customer

Input & Output

Example 1 — Basic Case
Input Table:
order_number customer_number
1 1
2 2
3 3
4 3
Output:
customer_number
3
💡 Note:

Customer 3 has placed 2 orders (order_number 3 and 4), while customers 1 and 2 have each placed only 1 order. Therefore, customer 3 is returned as the customer with the largest number of orders.

Example 2 — Single Customer Dominant
Input Table:
order_number customer_number
1 5
2 5
3 5
4 1
5 2
Output:
customer_number
5
💡 Note:

Customer 5 has placed 3 orders, which is more than any other customer. Customer 1 and 2 have each placed only 1 order, making customer 5 the clear winner.

Constraints

  • 1 ≤ order_number ≤ 500
  • 1 ≤ customer_number ≤ 500
  • All order_number are unique
  • Exactly one customer will have the most orders

Visualization

Tap to expand
Customer Order Analysis - Find Most Active CustomerOrders Tableorder_numbercustomer_number11223343GROUP BYcustomer_numberCOUNT(*)Aggregated Datacustomer_numberorder_count321121ORDER BYCOUNT DESCLIMIT 1Resultcustomer_number3Customer 3 placed the most orders (2 orders)
Understanding the Visualization
1
Input
Orders table with order and customer data
2
Group & Count
GROUP BY customer_number and COUNT(*)
3
Find Max
ORDER BY count DESC, LIMIT 1
Key Takeaway
🎯 Key Insight: Use GROUP BY with aggregate functions to analyze patterns in relational data
Asked in
Amazon 15 Facebook 12 Google 8
23.5K Views
High Frequency
~8 min Avg. Time
891 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