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.

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

Visualization

Tap to expand
๐Ÿ“Š Sales1|100|2023|5|9992|200|2023|3|293|100|2024|2|1099๐Ÿ“ฆ Products100|Laptop200|Mouse300|Keyboard๐Ÿ” Hash Index100 โ†’ Laptop200 โ†’ Mouse๐ŸŽฏ Join Resultsproduct_name | year | priceLaptop | 2023 | 999Mouse | 2023 | 29Laptop | 2024 | 1099BUILDPROBE & JOIN
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.
Asked in
Amazon 45 Microsoft 38 Google 32 Meta 28
89.5K Views
Very High Frequency
~8 min Avg. Time
2.8K 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