51. Sales Analysis
A company tracks product sales over different years. Each sale record includes the product ID, year of sale, quantity sold, and price per unit, the company wants to analyze yearly sales trends.
Your task is to generate this sales report by joining product names with sales data.
The company maintains two tables:
Sales table, which contains:
āāāāāāāāāāāāāāā¦āāāāāāāāāāā ā Column name ā Ā Type Ā ā ā āāāāāāāāāāāāāā¬āāāāāāāāāā⣠ā Ā sale_id Ā ā Ā int Ā Ā ā āāāāāāāāāāāāāāā¼āāāāāāāāāāā ā product_id Ā ā Ā int Ā Ā ā āāāāāāāāāāāāāāā¼āāāāāāāāāāā ā Ā Ā year Ā Ā ā Ā int Ā Ā ā āāāāāāāāāāāāāāā¼āāāāāāāāāāā ā Ā quantity Ā ā Ā int Ā Ā ā āāāāāāāāāāāāāāā¼āāāāāāāāāāā ā Ā Ā price Ā Ā ā Ā int Ā Ā ā āāāāāāāāāāāāāāā©āāāāāāāāāāā
- sale_id: A unique identifier for each sale transaction.
- product_id: The ID (foreign key) of the product being sold.
- year: The year in which the sale occurred.
- quantity: The number of units sold.
- price: The price per unit at the time of the sale.
Here, (sale_id, year) is the primary key (combination of columns with unique values) of this table.
Product table, which contains:
āāāāāāāāāāāāāāāā¦āāāāāāāāāāāā ā Column name Ā ā Ā Type Ā ā ā āāāāāāāāāāāāāāā¬āāāāāāāāāāā⣠ā Ā product_id Ā ā Ā int Ā Ā ā āāāāāāāāāāāāāāāā¼āāāāāāāāāāāā ā product_name ā varchar Ā ā āāāāāāāāāāāāāāāā©āāāāāāāāāāāā
- product_id: The unique ID (primary key) of each product.
- product_name: The name of the product.
Your task is to retrieve the product name, year of sale, and price per unit for each sale.
The result should be returned in any order.
Example 1:
Input:
Sales Table:
āāāāāāāāāāāā¦āāāāāāāāāāāāā¦āāāāāāāāāāā¦āāāāāāāāāāā¦āāāāāāāāāāā ā sale_id Ā ā product_id ā Ā year Ā ā quantity ā Ā price Ā ā ā āāāāāāāāāāā¬āāāāāāāāāāāāā¬āāāāāāāāāāā¬āāāāāāāāāāā¬āāāāāāāāāā⣠ā Ā Ā 1 Ā Ā ā Ā Ā 101 Ā Ā ā Ā 2008 Ā ā Ā Ā 10 Ā Ā ā Ā 5000 Ā ā āāāāāāāāāāāā¼āāāāāāāāāāāāā¼āāāāāāāāāāā¼āāāāāāāāāāā¼āāāāāāāāāāā ā Ā Ā 2 Ā Ā ā Ā Ā 101 Ā Ā ā Ā 2009 Ā ā Ā Ā 12 Ā Ā ā Ā 5000 Ā ā āāāāāāāāāāāā¼āāāāāāāāāāāāā¼āāāāāāāāāāā¼āāāāāāāāāāā¼āāāāāāāāāāā ā Ā Ā 3 Ā Ā ā Ā Ā 202 Ā Ā ā Ā 2011 Ā ā Ā Ā 15 Ā Ā ā Ā 9000 Ā ā āāāāāāāāāāāā©āāāāāāāāāāāāā©āāāāāāāāāāā©āāāāāāāāāāā©āāāāāāāāāāā
Product Table:
āāāāāāāāāāāāāā¦āāāāāāāāāāāāāāā ā product_id ā product_name ā ā āāāāāāāāāāāāā¬āāāāāāāāāāāāāā⣠ā Ā Ā 101 Ā Ā ā Ā Ā Sony Ā Ā ā āāāāāāāāāāāāāā¼āāāāāāāāāāāāāāā ā Ā Ā 202 Ā Ā ā Ā Ā Lenovo Ā Ā ā āāāāāāāāāāāāāā¼āāāāāāāāāāāāāāā ā Ā Ā 303 Ā Ā ā Ā Ā Dell Ā Ā ā āāāāāāāāāāāāāā©āāāāāāāāāāāāāāā
Output:
āāāāāāāāāāāāāāāā¦āāāāāāāāāāā¦āāāāāāāāāāā ā product_name ā Ā year Ā ā Ā price Ā ā ā āāāāāāāāāāāāāāā¬āāāāāāāāāāā¬āāāāāāāāāā⣠ā Ā Ā Sony Ā Ā ā Ā 2008 Ā ā Ā 5000 Ā ā āāāāāāāāāāāāāāāā¼āāāāāāāāāāā¼āāāāāāāāāāā ā Ā Ā Sony Ā Ā ā Ā 2009 Ā ā Ā 5000 Ā ā āāāāāāāāāāāāāāāā¼āāāāāāāāāāā¼āāāāāāāāāāā ā Ā Ā Lenovo Ā Ā ā Ā 2011 Ā ā Ā 9000 Ā ā āāāāāāāāāāāāāāāā©āāāāāāāāāāā©āāāāāāāāāāā
Explanation:
- Sony (product_id = 101) - Sold for 5000 in 2008 and 5000 in 2009.
- Lenovo (product_id = 202) - Sold for 9000 in 2011.
- Dell (product_id = 303) - No matching sale in Sales table, so it's excluded.
Still unsure what the problem is asking ?
Letās go through a few more examples, step by step, to make it clearer.