Watch 10 video solutions for Average Selling Price, a easy level problem involving Database. This walkthrough by Learn With Chirag has 18,750 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
Table: Prices
+---------------+---------+ | Column Name | Type | +---------------+---------+ | product_id | int | | start_date | date | | end_date | date | | price | int | +---------------+---------+ (product_id, start_date, end_date) is the primary key (combination of columns with unique values) for this table. Each row of this table indicates the price of the product_id in the period from start_date to end_date. For each product_id there will be no two overlapping periods. That means there will be no two intersecting periods for the same product_id.
Table: UnitsSold
+---------------+---------+ | Column Name | Type | +---------------+---------+ | product_id | int | | purchase_date | date | | units | int | +---------------+---------+ This table may contain duplicate rows. Each row of this table indicates the date, units, and product_id of each product sold.
Write a solution to find the average selling price for each product. average_price should be rounded to 2 decimal places. If a product does not have any sold units, its average selling price is assumed to be 0.
Return the result table in any order.
The result format is in the following example.
Example 1:
Input: Prices table: +------------+------------+------------+--------+ | product_id | start_date | end_date | price | +------------+------------+------------+--------+ | 1 | 2019-02-17 | 2019-02-28 | 5 | | 1 | 2019-03-01 | 2019-03-22 | 20 | | 2 | 2019-02-01 | 2019-02-20 | 15 | | 2 | 2019-02-21 | 2019-03-31 | 30 | +------------+------------+------------+--------+ UnitsSold table: +------------+---------------+-------+ | product_id | purchase_date | units | +------------+---------------+-------+ | 1 | 2019-02-25 | 100 | | 1 | 2019-03-01 | 15 | | 2 | 2019-02-10 | 200 | | 2 | 2019-03-22 | 30 | +------------+---------------+-------+ Output: +------------+---------------+ | product_id | average_price | +------------+---------------+ | 1 | 6.96 | | 2 | 16.96 | +------------+---------------+ Explanation: Average selling price = Total Price of Product / Number of products sold. Average selling price for product 1 = ((100 * 5) + (15 * 20)) / 115 = 6.96 Average selling price for product 2 = ((200 * 15) + (30 * 30)) / 230 = 16.96
Problem Overview: Each product has price intervals in the Prices table and sales records in UnitsSold. For every product, calculate the average selling price by weighting the price with the number of units sold during each valid price period.
The key detail: a sale only uses the price that was active on the purchase date. After matching sales to the correct price range, compute SUM(price * units) / SUM(units) for each product. If a product has no sales, the result should be 0.
Approach 1: Using SQL for Data Retrieval and Calculation (O(n))
This approach relies on relational operations directly in SQL. Join Prices and UnitsSold on product_id and filter rows where purchase_date falls between start_date and end_date. After the join, compute the weighted average using SUM(price * units) divided by SUM(units), grouped by product_id. A LEFT JOIN ensures products without sales still appear, and COALESCE or conditional aggregation handles division when no units exist.
This is the natural solution for database problems. The database engine handles filtering, joins, and aggregation efficiently. Time complexity is O(n) relative to the number of rows processed in the join, and space complexity is O(1) beyond query execution buffers.
Approach 2: Using In-Memory Data Structures (O(p + s))
If the data is loaded into application code, replicate the join logic manually. Store price intervals for each product using a hash map keyed by product_id. For every sale in UnitsSold, iterate through that product’s price intervals and find the range containing the purchase_date. Multiply the matched price by the number of units and accumulate totals for revenue and units.
After processing all sales, compute the weighted average for each product: total_revenue / total_units. This approach uses a hash map to group product data and simple iteration to locate valid price ranges. Time complexity is O(p + s) where p is price intervals and s is sales records, with O(p) space for storing intervals.
Recommended for interviews: Interviewers expect the SQL aggregation approach. It shows you understand joins, date filtering, and weighted averages in relational queries. The in-memory solution demonstrates the underlying logic and is useful when solving the problem in languages like Python or JavaScript outside a database environment.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| SQL Join + Aggregation | O(n) | O(1) | Best when solving directly in SQL or database interview questions |
| In-Memory Hash Map + Interval Check | O(p + s) | O(p) | When processing database rows in application code (Python/JavaScript) |