Interview Questions/SQL/Product Sales Analysis I

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 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_nameyearprice
Nokia20085000
Nokia20095000
Apple20119000

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_idproduct_idyearquantityprice
11002008105000
21002009125000
72002011159000

Product

product_idproduct_name
100Nokia
200Apple
300Samsung

Output

product_nameyearprice
Nokia20085000
Nokia20095000
Apple20119000