SQL Interview Questions – Product’s Price for Each Store

Spread the love

Problem Description –

Write an SQL query to find the price of each product in each store.

Return the result table in any order.

The query result format is in the following example.

Problem Link – Product price

Difficulty Level – Easy

Solution –

SELECT
    product_id,
    SUM(CASE WHEN store = 'store1' THEN price ELSE NULL END) as store1,
    SUM(CASE WHEN store = 'store2' THEN price ELSE NULL END) as store2,
    SUM(CASE WHEN store = 'store3' THEN price ELSE NULL END) as store3
FROM Products
GROUP BY 1

Rating: 1 out of 5.

Leave a Reply