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.

Hints

0
Ā 
Test Case

Input:

Sales
Product