diff --git a/README.md b/README.md index ac0317e..d7db70e 100644 --- a/README.md +++ b/README.md @@ -144,6 +144,7 @@ Useful for preparing for technical interviews and improving your SQL skills. - [1731. The Number of Employees Which Report to Each Employee](./leetcode/easy/1731.%20The%20Number%20of%20Employees%20Which%20Report%20to%20Each%20Employee.sql) - [1741. Find Total Time Spent by Each Employee](./leetcode/easy/1741.%20Find%20Total%20Time%20Spent%20by%20Each%20Employee.sql) - [1789. Primary Department for Each Employee](./leetcode/easy/1789.%20Primary%20Department%20for%20Each%20Employee.sql) + - [1795. Rearrange Products Table](./leetcode/easy/1795.%20Rearrange%20Products%20Table.sql) - [1965. Employees With Missing Information](./leetcode/easy/1965.%20Employees%20With%20Missing%20Information.sql) - [1978. Employees Whose Manager Left the Company](./leetcode/easy/1978.%20Employees%20Whose%20Manager%20Left%20the%20Company.sql) - [2356. Number of Unique Subjects Taught by Each Teacher](./leetcode/easy/2356.%20Number%20of%20Unique%20Subjects%20Taught%20by%20Each%20Teacher.sql) diff --git a/leetcode/easy/1795. Rearrange Products Table.sql b/leetcode/easy/1795. Rearrange Products Table.sql new file mode 100644 index 0000000..b3f6cf1 --- /dev/null +++ b/leetcode/easy/1795. Rearrange Products Table.sql @@ -0,0 +1,48 @@ +/* +Question 1795. Rearrange Products Table +Link: https://leetcode.com/problems/rearrange-products-table/description/?envType=problem-list-v2&envId=database + +Table: Products + ++-------------+---------+ +| Column Name | Type | ++-------------+---------+ +| product_id | int | +| store1 | int | +| store2 | int | +| store3 | int | ++-------------+---------+ +product_id is the primary key (column with unique values) for this table. +Each row in this table indicates the product's price in 3 different stores: store1, store2, and store3. +If the product is not available in a store, the price will be null in that store's column. + + +Write a solution to rearrange the Products table so that each row has (product_id, store, price). If a product is not available in a store, do not include a row with that product_id and store combination in the result table. + +Return the result table in any order. +*/ + +SELECT + product_id, + 'store1' AS store, + store1 AS price +FROM Products +WHERE store1 IS NOT NULL + +UNION ALL + +SELECT + product_id, + 'store2' AS store, + store2 AS price +FROM Products +WHERE store2 IS NOT NULL + +UNION ALL + +SELECT + product_id, + 'store3' AS store, + store3 AS price +FROM Products +WHERE store3 IS NOT NULL