SQLSQL
#24

Weighted Average Selling Price in SQL

MediumAmazon
Interview Question #24
Asked at Amazon

Given `prices` (product_id, start_date, end_date, price) and `units_sold` (product_id, purchase_date, units), calculate the average selling price for each product rounded to 2 decimal places. If a product has no sales, return 0.

Input Table: prices & units_sold
2 rows preview
product_idpriceunitspurchase_datestart_dateend_date
151002026-02-252026-02-202026-02-28
120152026-03-052026-03-012026-03-10
Expected Output Structure1 rows
product_idaverage_price
16.96
Interview Context

Asked frequently in data analyst and business analyst technical rounds. Focus on clean filtering, optimal indexing usage, and unambiguous column selection.

SQLPostgreSQL 15
Ready to execute

Click "Run Query" or press Ctrl + Enter to test your query.

Output will be verified against the test dataset.

Chat with us