Return All Sales in Each Product’s Earliest Year

Quick Overview

Return all sales recorded in each product's earliest year, retaining ties and repeated output rows.

Return All Sales in Each Product’s Earliest Year

Company: Amazon

Role: Software Engineer

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Other

### Problem For each product_id, find the first year present in Sales and return every sales entry for that product in that year. Output product_id, first_year, quantity and price in any order. Multiple sales in the same earliest year must all remain. ### Tables Table: `Sales` ```text +-------------+-------+ | Column Name | Type | +-------------+-------+ | sale_id | int | | product_id | int | | year | int | | quantity | int | | price | int | +-------------+-------+ (sale_id, year) is the primary key (combination of columns with unique values) of this table. Each row records a sale of a product in a given year. A product may have multiple sales entries in the same year. Note that the per-unit price. ``` ### Example ```text Sales (sale_id,product_id,year,quantity,price): (1,8,2020,2,30), (2,8,2020,5,28), (3,8,2021,7,25), (4,9,2022,1,80) Output (product_id,first_year,quantity,price): (8,2020,2,30), (8,2020,5,28), (9,2022,1,80) ``` ### What a Strong Answer Covers - Identify each product's earliest recorded sales year. - Return every sale for that year and retain rows with identical output fields. ### Follow-up Questions - How would you report totals for the earliest year instead of individual sales? - How would you compare sales in each product's first two recorded years?

Overview: Return all sales recorded in each product's earliest year, retaining ties and repeated output rows.

Amazon logo
Amazon
mediumSoftware EngineerOtherData Manipulation (SQL/Python)
0
0

Problem

For each product_id, find the first year present in Sales and return every sales entry for that product in that year. Output product_id, first_year, quantity and price in any order. Multiple sales in the same earliest year must all remain.

Tables

Table: Sales

+-------------+-------+
| Column Name | Type  |
+-------------+-------+
| sale_id     | int   |
| product_id  | int   |
| year        | int   |
| quantity    | int   |
| price       | int   |
+-------------+-------+
(sale_id, year) is the primary key (combination of columns with unique values) of this table.
Each row records a sale of a product in a given year.
A product may have multiple sales entries in the same year.
Note that the per-unit price.

Example

Sales (sale_id,product_id,year,quantity,price): (1,8,2020,2,30), (2,8,2020,5,28), (3,8,2021,7,25), (4,9,2022,1,80)
Output (product_id,first_year,quantity,price): (8,2020,2,30), (8,2020,5,28), (9,2022,1,80)

What a Strong Answer Covers Guidance

  • Identify each product's earliest recorded sales year.
  • Return every sale for that year and retain rows with identical output fields.

Follow-up Questions Guidance

  • How would you report totals for the earliest year instead of individual sales?
  • How would you compare sales in each product's first two recorded years?