SQL Interview Questions – Fix Product Name Format

Spread the love

Problem Description –

Since table Sales was filled manually in the year 2000, product_name may contain leading and/or trailing white spaces, also they are case-insensitive.

Write an SQL query to report

  • product_name in lowercase without leading or trailing white spaces.
  • sale_date in the format ('YYYY-MM').
  • total the number of times the product was sold in this month.

Return the result table ordered by product_name in ascending order. In case of a tie, order it by sale_date in ascending order.

The query result format is in the following example.

Problem Link – Fix Product Name Format

Difficulty Level – Easy

Solution –

SELECT
    LOWER(TRIM(product_name)) as product_name,
    DATE_FORMAT(sale_date, '%Y-%m') as sale_date,
    COUNT(sale_id) as total
FROM sales
GROUP BY 1, 2
ORDER BY 1, 2

Rating: 1 out of 5.

Leave a Reply