Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 0e0478c

Browse files
committed
Add a question
1 parent 58e1824 commit 0e0478c

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

‎README.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1903,3 +1903,50 @@ WHERE
19031903
---
19041904

19051905
**[⬆ 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

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /