Product Sales Analysis I - Problem
You're working for a business intelligence team that needs to analyze product sales data. You have two important tables:
- Sales Table: Contains transaction records with sale IDs, product IDs, years, quantities, and unit prices
- Product Table: Contains product information mapping product IDs to product names
Your task is to create a comprehensive sales report that shows what products were sold, when they were sold, and at what price.
Goal: Write a SQL query to join these tables and return the product_name, year, and price for each sale record.
The sales manager wants to see all sales transactions with readable product names instead of cryptic product IDs.
| Sales Table | |
|---|---|
| sale_id (int) | product_id (int) |
| year (int) | quantity (int) |
| price (int) | |
| Product Table | |
|---|---|
| product_id (int) | product_name (varchar) |
Input & Output
basic_example.sql โ SQL Query
$
Input:
Sales: [(1,100,2023,5,999), (2,200,2023,3,29), (3,100,2024,2,1099)]
Product: [(100,'Laptop'), (200,'Mouse'), (300,'Keyboard')]
โบ
Output:
[('Laptop',2023,999), ('Mouse',2023,29), ('Laptop',2024,1099)]
๐ก Note:
Each sales record is joined with its corresponding product. Sale ID 1 (product 100) matches with 'Laptop', Sale ID 2 (product 200) matches with 'Mouse', and Sale ID 3 (product 100) matches with 'Laptop' again.
single_product.sql โ SQL Query
$
Input:
Sales: [(1,100,2023,10,1200)]
Product: [(100,'Gaming Laptop')]
โบ
Output:
[('Gaming Laptop',2023,1200)]
๐ก Note:
Simple case with only one sales record and one product. The join produces a single result row with the product name, year, and price.
multiple_years.sql โ SQL Query
$
Input:
Sales: [(1,100,2022,1,800), (2,100,2023,1,900), (3,100,2024,1,1000)]
Product: [(100,'Laptop')]
โบ
Output:
[('Laptop',2022,800), ('Laptop',2023,900), ('Laptop',2024,1000)]
๐ก Note:
Same product sold across different years at different prices. Each sales record maintains its individual year and price information after the join.
Visualization
Tap to expand
Understanding the Visualization
1
Table Preparation
Database loads both Sales and Product tables into memory
2
Hash Table Creation
Build hash table from smaller Product table using product_id as key
3
Sales Record Processing
For each Sales record, lookup matching Product using hash table
4
Result Generation
Combine matched records and output selected columns
Key Takeaway
๐ฏ Key Insight: SQL INNER JOIN leverages hash tables for O(1) lookups, making it far more efficient than Cartesian products for combining related data from multiple tables.
Time & Space Complexity
Time Complexity
O(n + m)
With proper indexing, hash join processes each table once
โ Linear Growth
Space Complexity
O(min(n,m))
Hash table built for smaller table, then probe with larger
โก Linearithmic Space
Constraints
- 1 โค Sales table rows โค 105
- 1 โค Product table rows โค 104
- All product_id values in Sales table exist in Product table
- Primary key constraint: (sale_id, year) is unique in Sales table
- product_id is primary key in Product table
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code