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 Date - SQL OverviewInput: Activities Tablesell_dateproduct2020-05-30Headphone2020-05-30Basketball2020-06-01Pencil2020-06-02Mask2020-06-02MaskContains Duplicates!GROUP BY+ AggregationOutput: Daily Summarysell_datenum_soldproducts2020-05-302Basketball,Headphone2020-06-011Pencil2020-06-021MaskDuplicates Removed!Products Sorted!
Understanding the Visualization
1
Input
Raw sales data with duplicates
2
GROUP BY
Group by sell_date and aggregate
3
Output
Daily summary with product counts and names
Key Takeaway
🎯 Key Insight: Use GROUP BY with aggregate functions to transform raw transactional data into meaningful daily summaries
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