Pandas, Polars, PostgreSQL
Its better to solve Game Play Analysis IV first (Solution, 2nd Logic used here)\n- No need INNER JOIN between these 2 tables because product_id in sales table i
- Its better to solve [Game Play Analysis IV](https://leetcode.com/problems/game-play-analysis-iv/description/) first ([Solution](https://leetcode.com/problems/game-play-analysis-iv/solutions/5996925/pandas-polars-postgresql), `2nd Logic` used here)\n- No need `INNER JOIN between these 2 tables` because `product_id` in `sales` table is `a foreign key` which means every `product_id in \'sales\' table` `100% exists in \'product\' table`. We just have to take care the `sales` table now.\n# Pandas\n```py []\nimport pandas as pd\n\ndef sales_analysis(sales: pd.DataFrame, product: pd.DataFrame) -> pd.DataFrame:\n    return ( sales.assign(first_year = sales.groupby(by=\'product_id\') [\'year\'].transform(\'min\'))\n            .query("year == first_year")\n            .loc[:, [\'product_id\', \'first_year\', \'quantity\', \'price\']]\n           )\n```\n```py []\nimport pandas as pd\n\ndef sales_analysis(sales: pd.DataFrame, product: pd.DataFrame) -> pd.DataFrame:\n    return ( sales.assign(first_year = sales.groupby(by=\'product_id\') [\'year\'].transform(\'min\'))\n            .query("year == first_year")\n            .filter(items=[\'product_id\', \'first_year\', \'quantity\', \'price\'], axis=1)\n           ) # filter(..) here selects the columns.\n```\n\n# Polars\n```py\nimport polars as pl\nfrom polars import col\n\ndef sales_analysis(sales: pl.LazyFrame, product: pl.LazyFrame) -> pl.LazyFrame:\n    return ( sales.with_columns( first_year = col(\'year\').min() .over(partition_by=\'product_id\') )\n            .filter(col(\'year\') == col(\'first_year\'))\n            .select(col(\'product_id\', \'first_year\', \'quantity\', \'price\'))\n           )\n\nsales_analysis(pl.LazyFrame(sales), pl.LazyFrame(product)) .collect()\n```\n\n# PostgreSQL (Execution Order : WITH -> FROM -> WHERE -> SELECT)\n```postgresql\n-- Write your PostgreSQL query statement below\nWITH sales1 AS (\n\t\tSELECT *, MIN(sales.year) OVER (PARTITION BY product_id) AS "first_year"\n\t    FROM sales\n) -- It\'s a DERIVED TABLE not subquery.\n\nSELECT product_id, first_year, quantity, price\nFROM sales1\nWHERE "year" = first_year;\n```
['PostgreSQL', 'Pandas']