CMD Guide
HomeDatabasesSQL Practice Problems

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:

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.

diagram
diagram

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:

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

diagram
diagram

Common pitfalls

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.

🎨 Explain it visually

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.
🤔 Walk me through it (interactive)

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.
🧪 Quiz me & fix my gaps

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.
🧠 Make it stick

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.

📝 My notes