@@ -1903,3 +1903,50 @@ WHERE
1903
1903
---
1904
1904
1905
1905
** [ ⬆ Back to Top] ( #sql-coding-challenges-for-beginners ) **
1906
+
1907
+ ## 53. Products' Price for Each Store
1908
+
1909
+ Write a SQL query to find the price of each product in each store. Return the result table sorted in any order. Here is an example:
1910
+
1911
+ ```
1912
+ Products table:
1913
+ +-------------+--------+-------+
1914
+ | product_id | store | price |
1915
+ +-------------+--------+-------+
1916
+ | 0 | store1 | 95 |
1917
+ | 0 | store3 | 105 |
1918
+ | 0 | store2 | 100 |
1919
+ | 1 | store1 | 70 |
1920
+ | 1 | store3 | 80 |
1921
+ +-------------+--------+-------+
1922
+ ```
1923
+
1924
+ ```
1925
+ Result table:
1926
+ +-------------+--------+--------+--------+
1927
+ | product_id | store1 | store2 | store3 |
1928
+ +-------------+--------+--------+--------+
1929
+ | 0 | 95 | 100 | 105 |
1930
+ | 1 | 70 | null | 80 |
1931
+ +-------------+--------+--------+--------+
1932
+ ```
1933
+
1934
+ <details ><summary >Solution</summary >
1935
+
1936
+ ``` sql
1937
+ SELECT
1938
+ product_id,
1939
+ SUM (CASE WHEN store = ' store1' THEN price ELSE null END) AS store1,
1940
+ SUM (CASE WHEN store = ' store2' THEN price ELSE null END) AS store2,
1941
+ SUM (CASE WHEN store = ' store3' THEN price ELSE null END) AS store3
1942
+ FROM
1943
+ Products
1944
+ GROUP BY
1945
+ product_id;
1946
+ ```
1947
+
1948
+ </details >
1949
+
1950
+ ---
1951
+
1952
+ ** [ ⬆ Back to Top] ( #sql-coding-challenges-for-beginners ) **
0 commit comments