Orders With Maximum Quantity Above Average - Problem

You're managing an e-commerce platform and need to identify imbalanced orders - orders where customers have ordered an unusually high quantity of one particular product.

Given a table OrdersDetails with columns:

  • order_id (int): The unique identifier for each order
  • product_id (int): The product identifier
  • quantity (int): Number of units ordered

An order is considered imbalanced if its maximum quantity (highest quantity of any single product) is strictly greater than its average quantity.

The average quantity of an order = (total quantity of all products) ÷ (number of different products)

Goal: Find all order IDs that represent imbalanced orders.

Example: If an order contains 3 products with quantities [10, 2, 3], then:
• Maximum quantity = 10
• Average quantity = (10+2+3)/3 = 5
• Since 10 > 5, this is an imbalanced order

Input & Output

example_1.sql — Basic Case
$ Input: OrdersDetails = [[1,11,50],[1,22,10],[1,33,5],[2,44,4],[2,55,4],[3,66,5],[3,77,1],[3,88,2],[3,99,10]]
Output: [1, 3]
💡 Note: Order 1: quantities=[50,10,5], max=50, avg=(50+10+5)/3=21.67, since 50>21.67 it's imbalanced. Order 2: quantities=[4,4], max=4, avg=4, since 4=4 it's balanced. Order 3: quantities=[5,1,2,10], max=10, avg=(5+1+2+10)/4=4.5, since 10>4.5 it's imbalanced.
example_2.sql — All Balanced
$ Input: OrdersDetails = [[1,10,5],[1,20,5],[2,30,3],[2,40,3],[2,50,3]]
Output: []
💡 Note: Order 1: quantities=[5,5], max=5, avg=5, since 5=5 it's balanced. Order 2: quantities=[3,3,3], max=3, avg=3, since 3=3 it's balanced. No imbalanced orders found.
example_3.sql — Single Product Orders
$ Input: OrdersDetails = [[1,10,20],[2,20,15],[3,30,10]]
Output: []
💡 Note: Each order contains only one product. Order 1: quantities=[20], max=20, avg=20. Order 2: quantities=[15], max=15, avg=15. Order 3: quantities=[10], max=10, avg=10. Since max=avg for single-product orders, none are imbalanced.

Constraints

  • 1 ≤ order_id, product_id ≤ 100
  • 1 ≤ quantity ≤ 1000
  • Each (order_id, product_id) combination is unique
  • At least one order exists in the input

Visualization

Tap to expand
🛒 E-commerce Order Pattern DetectionOrder 1🎧×50 📱×10 💻×5Order 2📚×4 ✏️×4Order 3👕×5 👖×1 🧢×2 👟×10AnalysisMax: 50Avg: 21.7⚠️ IMBALANCEDAnalysisMax: 4Avg: 4.0✓ BALANCEDAnalysisMax: 10Avg: 4.5⚠️ IMBALANCED🔍 Business InsightImbalanced orders may indicate:• Bulk purchasing for resale• Inventory hoarding• Promotional abuse• Unusual buying patternsSQL: GROUP BY order_id HAVING MAX(qty) > AVG(qty)
Understanding the Visualization
1
Collect Orders
Gather all order data with product quantities
2
Group by Order
Organize products by their order ID
3
Calculate Statistics
Find max quantity and average quantity per order
4
Identify Imbalanced
Flag orders where max > average (suspicious patterns)
Key Takeaway
🎯 Key Insight: Use SQL's GROUP BY and aggregate functions to efficiently identify orders with unusual quantity distributions in a single query, enabling real-time fraud detection and business intelligence.
Asked in
Amazon 45 Shopify 35 eBay 28 Walmart 22
38.0K Views
Medium Frequency
~12 min Avg. Time
1.5K 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