Fix Product Name Format - Problem

Given a Sales table that contains product sales data from the year 2000, you need to clean and format the product names and aggregate sales data by month.

Table: Sales

Column NameType
sale_idint
product_namevarchar
sale_datedate

Requirements:

  • Clean product_name by removing leading/trailing whitespace and converting to lowercase
  • Format sale_date to 'YYYY-MM' format
  • Count total sales for each product per month
  • Order by product_name ascending, then by sale_date ascending

Table Schema

Sales
Column Name Type Description
sale_id PK int Unique identifier for each sale
product_name varchar Product name (may have whitespace and mixed case)
sale_date date Date when the product was sold
Primary Key: sale_id
Note: Table was filled manually in 2000, so product names may have inconsistent formatting

Input & Output

Example 1 — Basic cleaning and grouping
Input Table:
sale_id product_name sale_date
1 LC Aloe vera 2000-01-01
2 LC Aloe vera 2000-01-15
3 CHAMOMILE 2000-02-01
4 Chamomile 2000-02-10
Output:
product_name sale_date total
chamomile 2000-02 2
lc aloe vera 2000-01 2
💡 Note:

The query cleans product names by removing whitespace and converting to lowercase. ' LC Aloe vera ' and 'LC Aloe vera' both become 'lc aloe vera'. Sales dates are formatted to month format, and sales are counted per product per month.

Example 2 — Single product multiple months
Input Table:
sale_id product_name sale_date
1 Green Tea 2000-03-01
2 GREEN TEA 2000-03-15
3 green tea 2000-04-01
Output:
product_name sale_date total
green tea 2000-03 2
green tea 2000-04 1
💡 Note:

All variations of 'Green Tea' are normalized to 'green tea'. The product appears in two different months (March and April), so we get separate rows for each month with their respective counts.

Constraints

  • 1 ≤ sale_id ≤ 1000
  • product_name contains only alphabetic characters, spaces, and common punctuation
  • All dates are from the year 2000
  • sale_date is in valid date format

Visualization

Tap to expand
Product Name Cleanup and Monthly AggregationMessy Input DataRaw Product Names" LC Aloe vera ""LC Aloe vera"" CHAMOMILE ""Chamomile"+ Mixed dates...TRIM() + LOWER()GROUP BY monthClean Monthly ResultsAggregated Saleschamomile | 2000-02 | 2lc aloe vera | 2000-01 | 2✓ Clean names✓ Monthly grouping✓ Proper sorting
Understanding the Visualization
1
Raw Data
Messy product names with whitespace
2
Clean & Group
String functions + aggregation
3
Monthly Summary
Clean names with counts
Key Takeaway
🎯 Key Insight: Use string functions with GROUP BY to clean and aggregate messy data in a single query
Asked in
Amazon 23 Microsoft 18 Google 15
25.8K Views
Medium Frequency
~12 min Avg. Time
890 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