Alibaba
Foundations Track · Easy · 15 min
15 Compressed Mean
-- You're trying to find the mean number of items per order on Alibaba, rounded to 1 decimal place using tables which includes information on the count of it...
Company labels are directional practice context, not official interview guidance.
Objective
Practice aggregation through a Alibaba-tagged business scenario.
Approach
Use this track to lock in clean query structure, basic filtering logic, and confidence with grouped output.
Company context
Company labels are directional practice context, not official interview guidance.
-- You're trying to find the mean number of items per order on Alibaba, rounded to 1 decimal place using tables which includes information on the count of items in each order (item_count table) and the corresponding number of orders for each item count (order_occurrences table). SELECT ROUND(SUM(item_count::decimal * order_occurrences) / SUM(order_occurrences), 1) as mean FROM items_per_order -- other approaches: -- 1. ROUND((SUM(order_occurrences * item_count) / SUM(order_occurrences))::numeric, 1) AS mean -- 2. ROUND(CAST(SUM(item_count*order_occurrences) / SUM(order_occurrences) AS DECIMAL),1) AS mean -- my approach: -- mean items per order -- round to 1 SELECT ROUND(SUM(item_count * order_occurrences) / SUM(order_occurrences), 1) as mean FROM items_per_order -- remarks : both item_count and order_occurrences are of integer type by default, which means that division will return an integer result. To ensure that the output is rounded to 1 decimal place, we can cast either column to a decimal type.
items_per_order
SQL workspace
Run queries against the protected question data, then submit once the result shape looks right.
Sign in to run SQL
Create a free account or sign in before running queries against protected question data.