Rearrange Products Table with MySQL
Intuition\n Describe your first thoughts on how to solve this problem. \nWe need to rearrange the table so that each row contains the product\'s ID, the store, 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nWe need to rearrange the table so that each row contains the product\'s ID, the store, and its price. We should not include a row for the products that are not available in a particular store.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n1. Select specific columns from the table.\n    > Asterisk (`*`) \u2192 This symbol specifies that the query should return all columns of the queried tables\n\n2. Combine the results of different queries into a single result set.\n    > `UNION` \u2192 This command combines the result-set of two or more `SELECT` statements. Every `SELECT` statement must have the same number of columns, similar data types, the same order of columns.\n\n3. Filter the results to only include rows where the price is not empty.\n    > `NULL` \u2192 This means that the field has no value\n\n    > `IS NOT NULL` \u2192 This operator tests for non-empty values\n\n# Complexity\n- Time complexity: $O(n)$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n`n` is the total number of rows in the table. This is because we process each row in the table.\n\n# Code\n```\nSELECT * FROM (\n  SELECT product_id, \'store1\' store, store1 price FROM Products\n  UNION\n  SELECT product_id, \'store2\' store, store2 price FROM Products\n  UNION\n  SELECT product_id, \'store3\' store, store3 price FROM Products\n) p\nWHERE price IS NOT NULL;\n```
['MySQL']