logo

Back to questions

Compressed Mode [Alibaba SQL Interview Question]

Medium

You are trying to find the most common (aka the ) number of items bought per order on Alibaba.

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 ().

In case of multiple item counts, display the in ascending order.

Table:

Column NameType
item_countinteger
order_occurrencesinteger

Example Input:

item_countorder_occurrences
1500
21000
3800
41000

Example Output:

mode
2
4

Explanation

The most common number of is 1000. Both item counts of 2 and 4 fit this.

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

PostgreSQL 14