Skip to main content

Command Palette

Search for a command to run...

LeetCode 1070: Product Sales Analysis III

Solving LeetCode 1070: Product Sales Analysis III Guide

Published
2 min read
V

I'm Varchasv, a Data Engineer working on enterprise data integration.

Currently on a 90-problem challenge to level up my technical skills and switch to a more development-focused role.

What I'm doing:

  • Solving 2 Leetcode problems daily (SQL + DSA).
  • Blogging about each problem.
  • Building in public.

My Goal - Land a better data engineering role by mid-2026.

Follow my journey !!

Date: February 13, 2026
Category: SQL
Time Taken: 15 minutes
Difficulty: Medium


Problem Statement

Write a solution to find all sales that occurred in the first year each product was sold.

  • For each product_id, identify the earliest year it appears in the Sales table.

  • Return all sales entries for that product in that year.

Return a table with the following columns: product_id, first_year, quantity, and price.

Link: Product Sales Analysis III


Approach 1 (Subquery):

  • We only need the sales made in the first year, so we will calculate the first_year using the MIN clause in the inner subquery.

  • In the outer query, we will select the required columns and filter the rows where the product_id and year match those in the inner query.

Solution Code:

SELECT product_id, year as first_year, quantity, price
FROM Sales
WHERE (product_id, year) IN (
    SELECT product_id, MIN(`year`) AS first_year
    FROM sales
    GROUP BY product_id
)

Approach 2 (Window Functions):

  • By using the window functions RANK() or DENSE_RANK(), we can assign ranks based on the year in ascending order, so the earliest year will have a rank of 1.

  • We will partition the data by each product_id and select sales with a rank of 1.

Solution Code:

SELECT product_id, year AS first_year, quantity, price
FROM (
    SELECT *, DENSE_RANK() OVER(PARTITION BY product_id ORDER BY year ASC) AS rnk
    FROM Sales
) AS first_sales
WHERE rnk = 1

Pattern: Subquery | Window Functions

Mistakes I Made: Brain was just not working, I was unable to think of the solution and I also interpreted the problem incorrectly.

Series: 90 Days of Data Engineering Progress: 43/90 problems completed

Tags: #DEQuest #LeetCode #SQL #DataEngineering #BuildInPublic

90 Days of Data Engineering

Part 46 of 48

Solving 90 problems over 18 weeks. Daily posts Mon-Fri. Goal: Switch to development role.

Up next

LeetCode 601: Human Traffic of Stadium

Solution Guide for LeetCode 601: Human Traffic of Stadium