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
Fix Product Name Format INPUT: Sales Table id product_name sale_date 1 " Widget " 2000-01-15 2 " GADGET" 2000-01-20 3 "Widget " 2000-02-10 4 "GADGET " 2000-02-05 Data Issues: Leading/trailing whitespace Inconsistent case (UPPER/lower) Need monthly aggregation ALGORITHM STEPS 1 TRIM whitespace TRIM(product_name) 2 LOWER case LOWER(trimmed_name) 3 FORMAT date DATE_FORMAT(sale_date, '%Y-%m') 4 GROUP and COUNT GROUP BY product, month COUNT(*) as total_sales SELECT LOWER(TRIM(product_name)), DATE_FORMAT(sale_date,'%Y-%m'), COUNT(*) as total_sales GROUP BY 1,2 ORDER BY 1,2 FINAL RESULT product_name month sales gadget 2000-01 1 gadget 2000-02 1 widget 2000-01 1 widget 2000-02 1 [OK] Names cleaned and lowercase [OK] Dates formatted YYYY-MM [OK] Sales counted per month [OK] Sorted by name, then date " WIDGET " --> "widget" "2000-01-15" --> "2000-01" Key Insight: String cleaning with TRIM() and LOWER() ensures consistent product matching across variations. DATE_FORMAT extracts year-month for grouping. Combining these with GROUP BY and COUNT(*) aggregates sales correctly. ORDER BY ensures predictable, sorted output for reporting. TutorialsPoint - Fix Product Name Format | Optimal Solution
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