Interview Questions/SQL/Product Sales Analysis III

Product Sales Analysis III

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. Write a SQL query to select the product_id, the year of its first sale (first_year), the quantity sold, and the price for each product's first year of sale. Return the result in any order.

Database Schema

Sales

Column NameTypeDescription
sale_idINTPart of primary key
product_idINTFK to Product
yearINTYear of sale (part of primary key)
quantityINTQuantity sold
priceINTPrice per unit

Product

Column NameTypeDescription
product_idINTPrimary key
product_nameVARCHARName of product

Example

Sales

sale_idproduct_idyearquantityprice
11002008105000
21002009125000
72002011159000

Product

product_idproduct_name
100Nokia
200Apple
300Samsung

Output

product_idfirst_yearquantityprice
1002008105000
2002011159000

Explanation:

Product 100 (Nokia) first appeared in 2008 with quantity=10, price=5000. Product 200 (Apple) first appeared in 2011 with quantity=15, price=9000. Product 300 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_idproduct_idyearquantityprice
11002008105000
21002009125000
72002011159000

Product

product_idproduct_name
100Nokia
200Apple
300Samsung

Output

product_idfirst_yearquantityprice
1002008105000
2002011159000