The Group Sold Products By The Date interview question is a SQL task where you are given an Activities table containing sell_date and product. You need to find for each date the number of distinct products sold and the names of those products. The product names should be sorted lexicographically and combined into a single comma-separated string. The final result should be ordered by sell_date.
Companies like Meta and Amazon ask this Database coding problem to test your ability to perform string aggregation in SQL. While standard aggregation functions like SUM or COUNT are basic, aggregating strings into a single delimited list requires knowledge of specific, often dialect-dependent, SQL functions. It evaluates your practical data reporting skills.
This problem relies on the String Aggregation pattern.
sell_date.COUNT(DISTINCT product) to get the number of unique products sold that day.GROUP_CONCAT in MySQL, STRING_AGG in PostgreSQL) to combine the distinct product names. You must also specify an ORDER BY clause within the aggregation function to ensure the list is lexicographically sorted.Activities:
For 2020-05-30:
GROUP_CONCAT(product) instead of GROUP_CONCAT(DISTINCT product), which includes duplicate products in the comma-separated list.ORDER BY product inside the GROUP_CONCAT function, leading to a random order of products in the string.STRING_AGG in a MySQL environment or vice versa, resulting in a syntax error.Memorize the syntax for GROUP_CONCAT (MySQL) and STRING_AGG (PostgreSQL/SQL Server). It is the only way to pivot multiple string rows into a single column, which is a very common requirement for generating tags or lists in dashboards.