logo

Back to questions

Compressed Mean [Alibaba SQL Interview Question]

Easy

You are trying to find the mean number of items bought per order on Alibaba, rounded to 1 decimal place.

However, instead of doing analytics on all Alibaba orders, you have access to a summary table, which describes how many items were in an order (), and the number of orders that had that many items ().

Table:

Column NameType
item_countinteger
order_occurrencesinteger

Example Input:

item_countorder_occurrences
1500
21000
3800
41000

There are 500 orders with 1 item in each order; 1000 orders with 2 items in each order; 800 orders with 3 items in each order.

Example Output:

mean
2.7

Explanation

Let's calculate the arithmetic average:

Total items =

Total orders =

Mean =

The dataset you are querying against may have different input & output - this is just an example!

PostgreSQL 14