Product Sales Analysis I - Problem

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 sale
  • product_id (int): Foreign key referencing the Product table
  • year (int): Year when the sale occurred
  • quantity (int): Number of units sold
  • price (int): Price per unit

The Product table contains product information with columns:

  • product_id (int): Primary key, unique identifier for each product
  • product_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

Sales
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
Primary Key: (sale_id, year)
Product
Column Name Type Description
product_id PK int Primary key, unique identifier for each product
product_name varchar Name of the product
Primary Key: product_id

Input & Output

Example 1 — Basic Product Sales Join
Input Tables:
Sales
sale_id product_id year quantity price
1 100 2008 10 5000
2 100 2009 12 5000
7 200 2011 15 9000
Product
product_id product_name
100 Nokia
200 Apple
300 Samsung
Output:
product_name year price
Nokia 2008 5000
Nokia 2009 5000
Apple 2011 9000
💡 Note:

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).

Example 2 — Single Product Multiple Sales
Input Tables:
Sales
sale_id product_id year quantity price
1 100 2008 10 5000
2 100 2009 20 4800
Product
product_id product_name
100 Nokia
Output:
product_name year price
Nokia 2008 5000
Nokia 2009 4800
💡 Note:

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

Visualization

Tap to expand
Product Sales Analysis I: Table Join OverviewSalessale_idproduct_idyear1100200872002011Productproduct_idproduct_name100Nokia200AppleJOINproduct_idResultproduct_nameyearpriceNokia20085000Apple20119000
Understanding the Visualization
1
Input Tables
Sales and Product tables with related data
2
JOIN Operation
Connect tables using product_id
3
Output
Combined result with product names
Key Takeaway
🎯 Key Insight: Use INNER JOIN when you need to combine data from related tables using a common key
Asked in
Amazon 12 Google 8 Microsoft 15
125.0K Views
High Frequency
~8 min Avg. Time
2.2K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen