Back to questions

SQL COUNT DISTINCT Practice Exercise SQL Interview Question

SQL COUNT DISTINCT Practice Exercise

SQL Interview Question

Assume you're given a table containing data on Amazon customers and their spending on products in different category. Write a query using to identify the number of unique products within each product category.

Table:

Column NameType
categorystring
productstring
user_idinteger
spenddecimal
transaction_datetimestamp

Example Sample Input:

categoryproductuser_idspendtransaction_date
appliancerefrigerator165246.0012/26/2021 12:00:00
appliancerefrigerator123299.9903/02/2022 12:00:00
appliancewashing machine123219.8003/02/2022 12:00:00
electronicsvacuum178152.0004/05/2022 12:00:00
electronicswireless headset156249.9007/08/2022 12:00:00
electronicsvacuum145189.0007/15/2022 12:00:00

Example Sample Output:

categorycount
appliance2
electronics2

Explanation:

Within the "appliance" category, the two unique products are refrigerator & washing machine. Within the electronics category, the two unique products are vacuum and wireless headset.

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

Difficulty

Easy

Input

Output