Group Sold Products By The Date - Problem

Given a table Activities that contains product sales data, write a SQL solution to find for each date:

  • The number of different products sold
  • The names of all products sold (sorted alphabetically)

The table may contain duplicate entries for the same product sold on the same date.

Requirements:

  • Group results by sell_date
  • Count distinct products per date
  • Concatenate product names in lexicographical order
  • Order final results by sell_date

Table Schema

Activities
Column Name Type Description
sell_date date Date when the product was sold
product varchar Name of the product sold
Note: No primary key - may contain duplicates for same product on same date

Input & Output

Example 1 — Multiple Products Per Date
Input Table:
sell_date product
2020-05-30 Headphone
2020-06-01 Pencil
2020-06-02 Mask
2020-05-30 Basketball
2020-06-01 Bible
2020-06-02 Mask
2020-06-02 T-Shirt
Output:
sell_date num_sold products
2020-05-30 2 Basketball,Headphone
2020-06-01 2 Bible,Pencil
2020-06-02 2 Mask,T-Shirt
💡 Note:

Notice how duplicate Mask entries on 2020-06-02 are counted only once. Products are sorted alphabetically: Basketball,Headphone, Bible,Pencil, and Mask,T-Shirt.

Example 2 — Single Product Per Date
Input Table:
sell_date product
2020-01-01 Apple
2020-01-02 Orange
2020-01-03 Apple
Output:
sell_date num_sold products
2020-01-01 1 Apple
2020-01-02 1 Orange
2020-01-03 1 Apple
💡 Note:

Each date has only one unique product, so num_sold is 1 for all dates. Same products can appear on different dates.

Constraints

  • 1 ≤ Activities.length ≤ 1000
  • sell_date is a valid date
  • product is a non-empty string

Visualization

Tap to expand
Group Sold Products By The Date INPUT: Activities Table sell_date product 2020-05-30 Headphone 2020-06-01 Pencil 2020-06-02 Mask 2020-05-30 Basketball 2020-06-01 Bible 2020-06-02 Mask 2020-05-30 T-Shirt Note: Mask appears twice on 2020-06-02 (duplicate!) 3 unique dates 7 rows total ALGORITHM STEPS 1 GROUP BY sell_date Organize rows by date 2 COUNT(DISTINCT product) Count unique products/date 3 GROUP_CONCAT() Concatenate product names 4 ORDER BY sell_date Sort results by date SELECT sell_date, COUNT(DISTINCT product), GROUP_CONCAT(DISTINCT product ORDER BY product) GROUP BY sell_date FINAL RESULT sell_date num products 2020-05-30 3 Basketball, Headphone, T-Shirt 2020-06-01 2 Bible, Pencil 2020-06-02 1 Mask OK - Grouped! Results show: - Unique product count - Products sorted A-Z - Dates in order Key Insight: GROUP_CONCAT with DISTINCT removes duplicates automatically. Combined with ORDER BY inside GROUP_CONCAT, it alphabetizes the product list. The DISTINCT in COUNT ensures accurate unique counts. This single query handles deduplication, counting, concatenation, and sorting efficiently. TutorialsPoint - Group Sold Products By The Date | Optimal Solution
Asked in
Amazon 15 Meta 12 Microsoft 8
28.5K Views
Medium Frequency
~12 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen