Hone logo
Hone
Problems

Analyzing Sales Performance by Region and Product Category

This challenge focuses on using the CASE statement in conjunction with aggregate functions (like SUM, AVG, COUNT) within SQL to analyze sales data. Understanding how to categorize data within queries and then aggregate it is crucial for generating meaningful business reports and identifying trends. You'll be tasked with writing SQL queries to derive insights about sales performance based on region and product category.

Problem Description

You are given a table named Sales with the following columns:

  • SaleID (INT): Unique identifier for each sale.
  • Region (VARCHAR): The region where the sale occurred (e.g., "North", "South", "East", "West").
  • ProductCategory (VARCHAR): The category of the product sold (e.g., "Electronics", "Clothing", "Home Goods").
  • SaleAmount (DECIMAL): The amount of the sale.

Your goal is to write SQL queries that answer specific business questions using CASE statements to categorize sales and aggregate functions to calculate relevant metrics. The queries should be efficient and accurate, handling potential edge cases like empty regions or product categories gracefully.

Key Requirements:

  • Use CASE statements to create new categories or groupings within the Sales table.
  • Employ aggregate functions (SUM, AVG, COUNT, etc.) to calculate metrics based on these categories.
  • Handle potential NULL values appropriately.
  • Ensure the queries are readable and well-formatted.

Expected Behavior:

The queries should return the results as specified in the examples. The results should be accurate and reflect the data in the Sales table. The queries should not produce errors even with edge cases.

Examples

Example 1:

Input:
Sales Table:
SaleID | Region | ProductCategory | SaleAmount
-------|--------|-----------------|-----------
1      | North  | Electronics     | 100.00
2      | South  | Clothing        | 50.00
3      | North  | Home Goods      | 75.00
4      | East   | Electronics     | 120.00
5      | West   | Clothing        | 60.00
6      | North  | Electronics     | 80.00
7      | South  | Home Goods      | 90.00
8      | East   | Clothing        | 40.00

Query:
SELECT
    Region,
    SUM(CASE WHEN ProductCategory = 'Electronics' THEN SaleAmount ELSE 0 END) AS ElectronicsSales,
    SUM(CASE WHEN ProductCategory = 'Clothing' THEN SaleAmount ELSE 0 END) AS ClothingSales
FROM Sales
GROUP BY Region;
Output:
Region | ElectronicsSales | ClothingSales
-------|------------------|---------------
North  | 180.00           | 0.00
South  | 0.00             | 50.00
East   | 120.00           | 40.00
West   | 0.00             | 60.00

Explanation: The query calculates the total sales for 'Electronics' and 'Clothing' in each region using CASE statements within the SUM aggregate function.

Example 2:

Input:
Sales Table:
SaleID | Region | ProductCategory | SaleAmount
-------|--------|-----------------|-----------
1      | North  | Electronics     | 100.00
2      | South  | Clothing        | 50.00
3      | North  | Home Goods      | 75.00
4      | East   | Electronics     | 120.00
5      | West   | Clothing        | 60.00
6      | North  | Electronics     | 80.00
7      | South  | Home Goods      | 90.00
8      | East   | Clothing        | 40.00

Query:
SELECT
    Region,
    AVG(CASE WHEN SaleAmount > 80 THEN SaleAmount ELSE NULL END) AS AverageHighValueSale
FROM Sales
GROUP BY Region;
Output:
Region | AverageHighValueSale
-------|----------------------
North  | 90.00
South  | NULL
East   | 120.00
West   | NULL

Explanation: This query calculates the average sale amount for sales greater than 80 in each region. CASE filters the sales, and AVG calculates the average of the filtered values. NULL is returned when no sales exceed 80 in a region.

Example 3:

Input:
Sales Table:
SaleID | Region | ProductCategory | SaleAmount
-------|--------|-----------------|-----------
1      | North  | Electronics     | 100.00
2      | South  | Clothing        | 50.00
3      | North  | Home Goods      | 75.00
4      | East   | Electronics     | 120.00
5      | West   | Clothing        | 60.00
6      | North  | Electronics     | 80.00
7      | South  | Home Goods      | 90.00
8      | East   | Clothing        | 40.00
9      | North  | NULL            | 20.00

Query:
SELECT
    Region,
    COUNT(CASE WHEN ProductCategory IS NOT NULL THEN 1 ELSE NULL END) AS SalesWithCategory
FROM Sales
GROUP BY Region;
Output:
Region | SalesWithCategory
-------|-------------------
North  | 3
South  | 2
East   | 2
West   | 1

Explanation: This query counts the number of sales that have a non-NULL product category for each region. The CASE statement filters out NULL values in the ProductCategory column before counting.

Constraints

  • The Sales table will contain at least 10 rows.
  • Region and ProductCategory values will be strings with a maximum length of 50 characters.
  • SaleAmount will be a positive decimal number.
  • Queries should execute within 5 seconds on a moderately sized dataset (up to 10,000 rows).
  • The SQL dialect is standard SQL and should be compatible with most relational database systems (e.g., PostgreSQL, MySQL, SQL Server).

Notes

  • Consider using COALESCE or ISNULL to handle NULL values gracefully.
  • Think about how to structure your CASE statements to efficiently categorize the data.
  • The problem is designed to test your understanding of CASE statements and aggregate functions in combination.
  • Focus on clarity and readability in your SQL code. Proper indentation and comments are encouraged.
  • You may need to write multiple queries to address different aspects of the problem. Each query should be self-contained and address a specific business question.
  • Remember to GROUP BY the appropriate columns to get the desired aggregation results.
  • Test your queries thoroughly with various input data, including edge cases.
Loading editor...
plaintext