Group Sold Products By The Date
Problem
Table Activities has two columns: sell_date (a date) and product (a varchar). There is no primary key, so the table may contain duplicate rows — the same product can appear more than once on the same date.
+-------------+---------+
| Column Name | Type |
+-------------+---------+
| sell_date | date |
| product | varchar |
+-------------+---------+For each date, find how many different products were sold and list their names. The product names for each date must be sorted lexicographically and returned as a single comma-separated string. Return the result ordered by sell_date.
The shape of the answer
Two things have to happen per date, and both lean on the same idea — collapsing duplicates:
- num_sold — the count of distinct products, so a product sold twice on one day still counts once.
- products — the distinct product names, sorted A→Z, glued together with commas.
Both are aggregates computed inside a GROUP BY sell_date. The only genuinely new tool here is the string-aggregation function that turns a column of values into one delimited string.
Solution (MySQL)
MySQL provides GROUP_CONCAT, which accepts DISTINCT, an internal ORDER BY, and a SEPARATOR — everything the problem asks for in one call:
SELECT sell_date,
COUNT(DISTINCT product) AS num_sold,
GROUP_CONCAT(DISTINCT product ORDER BY product ASC SEPARATOR ',') AS products
FROM Activities
GROUP BY sell_date
ORDER BY sell_date ASC;Reading it piece by piece:
COUNT(DISTINCT product)— number of different products that day.GROUP_CONCAT(DISTINCT product ORDER BY product ASC SEPARATOR ',')— the de-duplicated names, sorted, joined by commas. TheORDER BYinsideGROUP_CONCATorders the values within each group; it is independent of the outerORDER BY sell_date.GROUP BY sell_datedefines the groups; the finalORDER BY sell_datesorts the output rows.
The same answer on other engines
String aggregation is one of the least standardized corners of SQL — the function name and the syntax for ordering and de-duplicating differ by engine. The logic is identical; only the spelling changes.
PostgreSQL
Postgres calls it STRING_AGG(expression, delimiter). It accepts an aggregate ORDER BY and the DISTINCT keyword, so the direct one-line translation is valid:
SELECT sell_date,
COUNT(DISTINCT product) AS num_sold,
STRING_AGG(DISTINCT product, ',' ORDER BY product) AS products
FROM Activities
GROUP BY sell_date
ORDER BY sell_date;This works because the ORDER BY expression (product) is the same as the DISTINCT-ed argument (product). Postgres only rejects the combination when they differ — e.g. STRING_AGG(DISTINCT product, ',' ORDER BY sell_date) raises “in an aggregate with DISTINCT, ORDER BY expressions must appear in argument list.” Since we sort by the very column we are de-duplicating, there is no conflict and no subquery is needed.
SQLite
SQLite calls it group_concat(expression, delimiter) and accepts DISTINCT, but it has no aggregate ORDER BY syntax at all — you cannot write an ORDER BY inside the function regardless of whether DISTINCT is present. To get sorted output, order the rows in a subquery first, then aggregate:
SELECT sell_date,
COUNT(DISTINCT product) AS num_sold,
group_concat(product, ',') AS products
FROM (SELECT DISTINCT sell_date, product
FROM Activities
ORDER BY sell_date, product) AS d
GROUP BY sell_date
ORDER BY sell_date;The inner SELECT DISTINCT … ORDER BY both de-duplicates and sorts; the outer group_concat then concatenates in that order. (Note: SQLite’s ordering-via-subquery relies on the optimizer preserving row order into the aggregate, which holds in practice but is not formally guaranteed by the SQL standard.)
Common pitfalls
- Forgetting
DISTINCT. Because the table allows duplicate rows, a missingDISTINCTover-countsnum_soldand repeats names in theproductsstring. - Confusing the two
ORDER BYclauses. The one inside the aggregate sorts names within a date; the outer one sorts the output rows by date. You need both. - Assuming string-aggregation syntax is portable. It is the function whose spelling varies most across engines — confirm the exact name (
GROUP_CONCATvsSTRING_AGGvsgroup_concat) and how each handles ordering before porting a query. - Over-engineering Postgres. You do not need a subquery to de-duplicate in Postgres here;
STRING_AGG(DISTINCT product, ',' ORDER BY product)is valid because the sort key matches the distinct argument.
Source & attribution
Problem adapted from LeetCode 1484, “Group Sold Products By The Date.” The MySQL reference solution follows the standard GROUP_CONCAT approach; the PostgreSQL and SQLite equivalents and the engine-portability notes were verified against the official documentation for each engine (MySQL GROUP_CONCAT, PostgreSQL string_agg aggregate-expression rules, and SQLite group_concat).
🤖 Don't fully get this? Learn it with Claude
Stuck on Group Sold Products By The Date? Open Claude, copy a block below, and it'll teach you this exact concept — visually and interactively.
Build the mental picture, not memorization.
I just read a lesson on **Group Sold Products By The Date** (Databases) and want to truly understand it. Explain Group Sold Products By The Date from first principles using ONE vivid real-world analogy and a visual mental model — draw it as ASCII art or a clear step-by-step diagram — with a concrete example using real numbers. Then ask me one question to check I got the mental picture, and wait for my reply. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
Socratic — adapts to where you're stuck.
Teach me **Group Sold Products By The Date** interactively. Ask me ONE guiding question at a time, wait for my answer, and adapt to my confusion — build the idea with me step by step instead of explaining it all at once. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
Active recall exposes what you missed.
Quiz me on **Group Sold Products By The Date** with 5 questions, easy to tricky, ONE at a time. Tell me if each answer is right; at the end, explain clearly what I got wrong and why. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
Intuition + hook + flashcards for long-term memory.
Help me remember **Group Sold Products By The Date** for the long term: give the one-sentence intuition, a memorable hook/mnemonic, a tiny worked example, and 3 active-recall flashcards (Q -> A). If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.