You are given two tables: Sales and Product.
The Sales table contains information about sales transactions with columns:
sale_id(int): Unique identifier for each saleproduct_id(int): Foreign key referencing the Product tableyear(int): Year when the sale occurredquantity(int): Number of units soldprice(int): Price per unit
The Product table contains product information with columns:
product_id(int): Primary key, unique identifier for each productproduct_name(varchar): Name of the product
Write a SQL query to report the product_name, year, and price for each sale_id in the Sales table. Return the result in any order.
Table Schema
| Column Name | Type | Description |
|---|---|---|
sale_id
PK
|
int | Unique identifier for each sale |
product_id
|
int | Foreign key referencing Product table |
year
PK
|
int | Year when the sale occurred |
quantity
|
int | Number of units sold |
price
|
int | Price per unit |
| Column Name | Type | Description |
|---|---|---|
product_id
PK
|
int | Primary key, unique identifier for each product |
product_name
|
varchar | Name of the product |
Input & Output
| sale_id | product_id | year | quantity | price |
|---|---|---|---|---|
| 1 | 100 | 2008 | 10 | 5000 |
| 2 | 100 | 2009 | 12 | 5000 |
| 7 | 200 | 2011 | 15 | 9000 |
| product_id | product_name |
|---|---|
| 100 | Nokia |
| 200 | Apple |
| 300 | Samsung |
| product_name | year | price |
|---|---|---|
| Nokia | 2008 | 5000 |
| Nokia | 2009 | 5000 |
| Apple | 2011 | 9000 |
The query joins the Sales table with the Product table on product_id. For each sale, we get the corresponding product name along with the year and price. Sale IDs 1 and 2 both reference product_id 100 (Nokia), while sale ID 7 references product_id 200 (Apple).
| sale_id | product_id | year | quantity | price |
|---|---|---|---|---|
| 1 | 100 | 2008 | 10 | 5000 |
| 2 | 100 | 2009 | 20 | 4800 |
| product_id | product_name |
|---|---|
| 100 | Nokia |
| product_name | year | price |
|---|---|---|
| Nokia | 2008 | 5000 |
| Nokia | 2009 | 4800 |
This example shows the same product (Nokia) being sold in different years with different prices. The JOIN operation correctly matches both sales records with the same product name.
Constraints
-
1 ≤ sale_id ≤ 1000 -
1 ≤ product_id ≤ 1000 -
2000 ≤ year ≤ 2030 -
1 ≤ quantity ≤ 1000 -
1 ≤ price ≤ 10000 -
1 ≤ product_name.length ≤ 100