Hone logo
Hone
Problems

Analyzing Product Sales by Category

Imagine you are working for an e-commerce company and need to understand how different product categories are performing. Your task is to aggregate sales data to find the total revenue generated by each product category. This is a fundamental task in business intelligence, allowing you to identify top-performing categories and areas for improvement.

Problem Description

You are given a table named sales that contains records of individual product sales. Each record includes information about the product sold, its category, and the sale price.

Your goal is to write a SQL query that calculates the total revenue for each unique product category.

Key Requirements:

  1. Group by Category: The results must be grouped by the category column.
  2. Calculate Total Revenue: For each category, sum up the sale_price of all products belonging to that category.
  3. Output Columns: The output should contain two columns:
    • category: The name of the product category.
    • total_revenue: The sum of sale_price for all sales within that category.
  4. Order Results: The results should be ordered by total_revenue in descending order.

Expected Behavior:

The query should process all records in the sales table and produce a summary table where each row represents a distinct product category and its corresponding total sales value.

Edge Cases to Consider:

  • Empty Table: If the sales table is empty, the query should return an empty result set.
  • Categories with No Sales: While not directly addressable with the given table structure, conceptually, if a category existed but had no sales entries, it would not appear in the output. The current problem focuses on categories with sales.

Examples

Example 1:

Input Table: sales

sale_idproduct_namecategorysale_price
1LaptopElectronics1200.00
2T-ShirtApparel25.00
3KeyboardElectronics75.00
4JeansApparel50.00
5MonitorElectronics300.00

Output:

categorytotal_revenue
Electronics1575.00
Apparel75.00

Explanation:

  • For the 'Electronics' category, the total revenue is 1200.00 + 75.00 + 300.00 = 1575.00.
  • For the 'Apparel' category, the total revenue is 25.00 + 50.00 = 75.00.
  • The results are ordered by total_revenue descending.

Example 2:

Input Table: sales

sale_idproduct_namecategorysale_price
1Book ABooks15.00
2Book BBooks20.00
3PenOffice3.00
4NotebookOffice5.00
5Book CBooks18.00
6StaplerOffice10.00

Output:

categorytotal_revenue
Books53.00
Office18.00

Explanation:

  • 'Books' total revenue: 15.00 + 20.00 + 18.00 = 53.00.
  • 'Office' total revenue: 3.00 + 5.00 + 10.00 = 18.00.
  • Ordered by total_revenue descending.

Example 3: (Edge Case - Empty Table)

Input Table: sales

sale_idproduct_namecategorysale_price

Output:

(Empty Result Set)

Explanation:

When the input table is empty, no categories can be formed, and thus no aggregated results are produced.

Constraints

  • The sales table will contain at least 0 rows.
  • The category column will be of a string type.
  • The sale_price column will be of a numeric type (e.g., DECIMAL, FLOAT, INTEGER).
  • The query should be efficient and execute within reasonable time limits, even for large datasets (assume up to 1 million rows).

Notes

  • You will need to use the GROUP BY clause to group rows based on the category.
  • An aggregate function will be necessary to sum the sale_price for each group.
  • Consider using ORDER BY to sort the final results.
  • The exact SQL syntax for aggregate functions and grouping may vary slightly between different SQL dialects, but the core logic remains the same.
Loading editor...
plaintext