PostgreSQL solution
Important notes\n\nThis query isn\u2019t difficult in terms of joins and aggregation. The trickiest part for me was handling NULL values using COALESCE. It took
# Important notes\n\nThis query isn\u2019t difficult in terms of joins and aggregation. The trickiest part for me was handling NULL values using COALESCE. It took me around 20 minutes to figure out how to correctly calculate the average price until ChatGPT suggested me to handle NULL values and convert the data type when necessary.\n\n\n# Code\n```postgresql []\n-- Write your PostgreSQL query statement below\n\nSELECT\n    p.product_id,\n    ROUND(COALESCE(SUM(P.price * COALESCE(U.units, 0)) / NULLIF(SUM(U.units), 0)::numeric, 0), 2) AS average_price\nFROM\n    Prices p\nLEFT JOIN\n    UnitsSold u\nON\n    p.product_id = u.product_id\nAND\n    u.purchase_date BETWEEN p.start_date AND p.end_date\nGROUP BY\n    p.product_id\n```
['PostgreSQL']