Fast and Simple Solution with description
Problem Statement\nGiven a table Products with columns product_id, store1, store2, and store3, representing the prices of a product in three different stores, t
# Problem Statement\nGiven a table `Products` with columns `product_id`, `store1`, `store2`, and `store3`, representing the prices of a product in three different stores, the task is to retrieve all available product prices along with the corresponding store information.\n\n## Intuition\nThe problem involves restructuring the data to present product prices along with their corresponding store information in a unified format.\n\n## Approach\n1. **Use of `UNION`:**\n   - Utilize the `UNION` operator to combine the prices of a product from each store into a single column.\n   - Create a subquery for each store to extract `product_id`, store information (e.g., \'store1\'), and the corresponding price column (e.g., `store1 AS price`).\n   - Combine the results of these subqueries using `UNION`.\n\n2. **Filtering Null Prices:**\n   - Use the outer query to filter out rows where the price is `NULL`. This ensures that only valid prices are included in the final result.\n\n## Complexity\n- **Time complexity:**\n  - The time complexity is primarily determined by the use of the `UNION` operator and the filtering operation. It is generally efficient and can be considered as $$O(n)$$, where $$n$$ is the number of rows in the `Products` table.\n\n- **Space complexity:**\n  - The space complexity is also $$O(n)$$, where $$n$$ is the number of rows in the `Products` table. This accounts for the storage of intermediate results during the use of `UNION`.\n\n# Code\n```\nSELECT *\nFROM (\n    SELECT product_id, \'store1\' AS store, store1 AS price \n    FROM Products\n\n    UNION\n\n    SELECT product_id, \'store2\' AS store, store2 AS price\n    FROM Products\n\n    UNION\n\n    SELECT product_id, \'store3\' AS store, store3 AS price\n    FROM Products\n) AS t\nWHERE price IS NOT NULL;\n```\n![image.png](https://assets.leetcode.com/users/images/b7e0eebd-bc9c-4c8e-86a8-a33d8f1d3300_1703322963.5883312.png)\n
['MySQL']