Product Sales Analysis I
Preview mode. Log in to edit, run, submit, and save progress.
Medium
Description
You have two tables: Sales and Product. Each row in Sales records a sale of a product in a given year at a given price. Write a SQL query to report the product_name, year, and price for each sale. Return the result in any order.
Database Schema
Sales
| Column Name | Type | Description |
|---|---|---|
| sale_id | INT | Part of primary key |
| product_id | INT | FK to Product |
| year | INT | Year of sale (part of primary key) |
| quantity | INT | Quantity sold |
| price | INT | Price per unit |
Product
| Column Name | Type | Description |
|---|---|---|
| product_id | INT | Primary key |
| product_name | VARCHAR | Name of product |
Example
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 |
Explanation:
Nokia (product_id=100) was sold in 2008 and 2009 for 5000. Apple (product_id=200) was sold in 2011 for 9000. Samsung has no sales.
Approach hint
Start with a simple approach, explain the trade-off, then move toward a cleaner or more scalable solution.
Common mistake
Skipping assumptions, edge cases, or trade-offs can make an otherwise good answer feel incomplete.
SQL Editor
Loading...
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 |