Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
48 changes: 48 additions & 0 deletions leetcode/easy/1795. Rearrange Products Table.sql
Original file line number Diff line number Diff line change
@@ -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