-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProduct Sales Analysis I.sql
More file actions
35 lines (34 loc) · 1.33 KB
/
Product Sales Analysis I.sql
File metadata and controls
35 lines (34 loc) · 1.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
-- Problem Statement: Write a solution to report the product_name, year, and price for each sale_id in the Sales table.
-- Given Table: Sales
--
-- +-------------+-------+
-- | Column Name | Type |
-- +-------------+-------+
-- | sale_id | int |
-- | product_id | int |
-- | year | int |
-- | quantity | int |
-- | price | int |
-- +-------------+-------+
-- (sale_id, year) is the primary key (combination of columns with unique values) of this table.
-- product_id is a foreign key (reference column) to the Product table.
-- Each row of this table shows a sale on the product product_id in a certain year.
-- Note that the price is per unit.
--
-- Given Table: Product
--
-- +--------------+---------+
-- | Column Name | Type |
-- +--------------+---------+
-- | product_id | int |
-- | product_name | varchar |
-- +--------------+---------+
-- product_id is the primary key (column with unique values) of this table.
-- Each row of this table indicates the product name of each product.
--
-- Approach: To report the product_name, year, and price for each sale_id in the Sales table, we can perform a LEFT JOIN between the Sales table and the Product table on the product_id column.
--
-- SQL Solution:
SELECT product_name, year, price
FROM Sales
LEFT JOIN Product ON Product.product_id = Sales.product_id;