Hone logo
Hone
Problems

Calculating Total Sales Revenue

Businesses often need to analyze sales data to understand overall performance. A common requirement is to calculate the total revenue generated from all sales transactions. This challenge focuses on using the SUM aggregate function in SQL to efficiently compute this total revenue from a sales table.

Problem Description

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

  • SaleID (INTEGER): A unique identifier for each sale.
  • ProductID (INTEGER): The ID of the product sold.
  • Quantity (INTEGER): The number of units sold in the transaction.
  • UnitPrice (DECIMAL): The price of a single unit of the product.

Your task is to write a SQL query that calculates the total revenue generated from all sales in the Sales table. The total revenue for a single sale is calculated as Quantity * UnitPrice. The final result should be the sum of these individual sale revenues.

Key Requirements:

  • The query must use the SUM aggregate function.
  • The query must correctly calculate the revenue for each sale (Quantity * UnitPrice).
  • The query must return a single value representing the total revenue.

Expected Behavior:

The query should return a single row with a single column named TotalRevenue, containing the sum of all sale revenues.

Edge Cases to Consider:

  • The Sales table might be empty. In this case, the query should return 0.
  • Quantity or UnitPrice could be zero. These should be handled correctly in the revenue calculation.
  • UnitPrice could be negative (e.g., a refund). The query should correctly sum these negative values.

Examples

Example 1:

Input:
Sales Table:
SaleID | ProductID | Quantity | UnitPrice
-------|-----------|----------|----------
1      | 101       | 2        | 10.00
2      | 102       | 1        | 25.00
3      | 101       | 3        | 10.00
Output:
TotalRevenue
------------
65.00

Explanation:

  • Sale 1 Revenue: 2 * 10.00 = 20.00
  • Sale 2 Revenue: 1 * 25.00 = 25.00
  • Sale 3 Revenue: 3 * 10.00 = 30.00
  • Total Revenue: 20.00 + 25.00 + 30.00 = 65.00

Example 2:

Input:
Sales Table:
SaleID | ProductID | Quantity | UnitPrice
-------|-----------|----------|----------
1      | 101       | 0        | 10.00
2      | 102       | 1        | 0.00
Output:
TotalRevenue
------------
0.00

Explanation:

  • Sale 1 Revenue: 0 * 10.00 = 0.00
  • Sale 2 Revenue: 1 * 0.00 = 0.00
  • Total Revenue: 0.00 + 0.00 = 0.00

Example 3:

Input:
Sales Table:
SaleID | ProductID | Quantity | UnitPrice
-------|-----------|----------|----------
1      | 101       | 2        | 10.00
2      | 102       | 1        | -25.00
Output:
TotalRevenue
------------
-5.00

Explanation:

  • Sale 1 Revenue: 2 * 10.00 = 20.00
  • Sale 2 Revenue: 1 * -25.00 = -25.00
  • Total Revenue: 20.00 + (-25.00) = -5.00

Constraints

  • The Sales table will contain at least 0 rows.
  • Quantity will be a non-negative integer.
  • UnitPrice will be a decimal number (can be positive, negative, or zero).
  • The database system used is assumed to support standard SQL aggregate functions.
  • The query should execute within a reasonable time (e.g., less than 1 second) even for a table with millions of rows.

Notes

  • Consider using aliases to make your query more readable.

  • The SUM function automatically handles NULL values by treating them as 0.

  • Focus on writing a concise and efficient query. Avoid unnecessary complexity.

  • Pseudocode:

    SELECT SUM(Quantity * UnitPrice) AS TotalRevenue
    FROM Sales;
    
Loading editor...
plaintext