Solution to the CORRECT version of this question
Intuition\nThe present version of the question, as well as it\'s test cases are incorrect. The product table is correctly present, but test cases do not seem to
# Intuition\nThe present version of the question, as well as it\'s test cases are incorrect. The product table is correctly present, but test cases do not seem to be using it properly.\n\n# Approach\nMy first solution went through all possible tests:\n```\nSELECT product_id, year AS first_year, quantity, price\nFROM sales\nWHERE (product_id, year) IN (\n    SELECT product_id, MIN(year)\n    FROM sales\n    GROUP BY 1\n);\n```\nThis solution should not have been correct. For the reason of existence of the product table, there could be a product that would not be sold (consider me nerdy, idc.). For that reason, the previous solution should not reveal all products the company had in their posession. \nTherefore, it makes sense to first left join the products table with sales, and then join it with the (product_id, year) table. Yes, 2 joins, but logically valid now - conciousness happy now:)\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\n-- Write your PostgreSQL query statement below\nSELECT \n    p.product_id, \n    COALESCE(year, 0) AS first_year, \n    COALESCE(quantity, 0) AS quantity, \n    COALESCE(price, 0) AS price\nFROM product p\nLEFT JOIN sales s\n    ON p.product_id = s.product_id\nWHERE (s.product_id, s.year) IN (\n    SELECT product_id, MIN(year)\n    FROM sales\n    GROUP BY 1\n);\n\n```
['PostgreSQL']