Naming the products on the sales sheet — SQL
For each sale, show the product's name alongside the sale's year and unit price, so the sheet stops listing bare product numbers.
SQL · Easy
The problem
The sales sheet only shows product numbers. Put names on it. A Sales table records each sale with the product sold, the year, the quantity and the unit price. Product names are kept in a separate table. For every sale, report the product name together with the year and the price of that sale.
The story
The finance team received a sales sheet that shows only product numbers, and nobody can tell what sold. Each sale records the product, the year, the quantity and the unit price, and product names live in a separate table. For every sale, list the product name with that sale's year and price.
What to return
- Three columns in this order: product_name, year, price. One row per row of Sales. Any order.
Constraints
- (sale_id, year) is unique in Sales
- product_id is unique in Product
- every product_id in Sales exists in Product
Example
- Input
Sales(sale_id, product_id, year, quantity, price)[1, 10, 2021, 4, 1200][2, 10, 2022, 6, 1250][5, 20, 2022, 3, 300]Product(product_id, product_name)[10, "Desk lamp"][20, "Notebook"][30, "Stapler"]- Output
(product_name, year, price)["Desk lamp", 2021, 1200]["Desk lamp", 2022, 1250]["Notebook", 2022, 300]
Stapler was never sold, so it does not appear.