🔥🔥🔥 3 solutions in MySQL | Beats 90.14% 🔥🔥🔥
Complexity\n- Time complexity: Runtime 1316 ms Beats 41.76%\n\n# Code\n\nSELECT \n\tp.product_name,\n    SUM(o.unit) AS unit\nFROM products p JOIN Orders o ON p
# 1. Solution with Join & GROUP BY with Runtime 762 ms (Beats 60.75%):\n    - Time complexity: O(N * M), here N = numbers of products, M = number of orders\n    - Space complexity: O(N + M + G), G = numbers of total unique products on orders table after satisfying WHERE condition\n    \n# Code\n``` MySQL []\nSELECT \n\tp.product_name,\n    SUM(o.unit) AS unit\nFROM products p JOIN Orders o ON p.product_id = o.product_id\nWHERE o.order_date >= \'2020-02-01\' AND o.order_date <= \'2020-02-29\'\nGROUP BY p.product_id\nHAVING unit >= 100;\n```\n\n# 2. Solution with Join, CTE & Window function with Runtime 763 ms (Beats 60.43%):\n    - Time complexity: O(N + M), here N = numbers of products, M = number of orders\n\t- Space complexity: O(M)\n    \n# Code\n``` MySQL []\nWITH filtered_Order AS (\n    SELECT \n        o.product_id,\n        SUM(o.unit) OVER (PARTITION BY o.product_id) AS total,\n        ROW_NUMBER() OVER (PARTITION BY o.product_id ORDER BY o.product_id) AS rn\n    FROM Orders o \n    WHERE o.order_date >= \'2020-02-01\' \n          AND o.order_date <= \'2020-02-29\'\n)\nSELECT \n    p.product_name,\n    r.total AS unit\nFROM filtered_Order r\nJOIN products p ON p.product_id = r.product_id\nWHERE r.rn = 1 \n  AND r.total >= 100;\n```\n\n# 3. Solution with Join, Sub-query & Group by with Runtime 669 ms (Beats 90.14%):\n    - Time complexity: O(M log M + N + k + K log K), here N = numbers of products, M = number of orders, K = size of the sub-query result\n\t- Space complexity: O(K), here K = numbers of unique product_id from sub-query result\n    \n# Code\n``` MySQL []\nSELECT \n    p.product_name,\n    o.total_units AS unit\nFROM products p\nJOIN (\n    SELECT \n        o.product_id,\n        SUM(o.unit) AS total_units\n    FROM Orders o\n    WHERE o.order_date >= \'2020-02-01\' \n          AND o.order_date <= \'2020-02-29\'\n    GROUP BY o.product_id\n    HAVING SUM(o.unit) >= 100\n) o ON p.product_id = o.product_id\nORDER BY p.product_name;\n```\n
['Database', 'MySQL']