{x}
blog image

Orders With Maximum Quantity Above Average

Solution Explanation:

This problem requires finding orders where the maximum quantity of a product within the order is greater than the average quantity across all orders. The solution uses a SQL query to achieve this efficiently.

Approach:

  1. Calculate Order Statistics: The core of the solution lies in a Common Table Expression (CTE) named t. This CTE groups the OrdersDetails table by order_id and calculates two key metrics for each order:

    • max_quantity: The maximum quantity of any product in the order using the MAX() aggregate function.
    • avg_quantity: The average quantity per product in the order. This is calculated as SUM(quantity) / COUNT(1). COUNT(1) counts the number of products in the order.
  2. Identify Imbalanced Orders: The final SELECT statement filters the results from the CTE t. It selects order_id only for those orders where max_quantity is strictly greater than the maximum average quantity across all orders. The subquery (SELECT MAX(avg_quantity) FROM t) efficiently finds the highest average quantity among all orders. Any order with a maximum quantity exceeding this value is considered imbalanced.

MySQL Code:

WITH
    t AS (
        SELECT
            order_id,
            MAX(quantity) AS max_quantity,
            SUM(quantity) / COUNT(1) AS avg_quantity
        FROM OrdersDetails
        GROUP BY order_id
    )
SELECT order_id
FROM t
WHERE max_quantity > (SELECT MAX(avg_quantity) FROM t);

Time Complexity Analysis:

The time complexity of this solution is dominated by the operations within the CTE t.

  • Grouping by order_id : O(N log N) or O(N) depending on the database implementation's sorting algorithm.
  • Calculating MAX() and SUM() for each group: O(N)
  • The final SELECT statement compares each order's max quantity to the overall max average quantity. This takes O(N) time, where N is the number of orders.

Therefore, the overall time complexity is O(N log N) or O(N), depending on the database system's specific optimization of the group by operation. In practice, database systems are highly optimized, and this query would likely execute very quickly even for large datasets.

Space Complexity Analysis:

The space complexity is determined primarily by the size of the CTE t. t stores the order_id, max_quantity, and avg_quantity for each order. Thus, the space complexity is O(M), where M is the number of unique orders in the OrdersDetails table. This is linear with respect to the number of unique orders.