(sale_id, year) is the primary key (combination of columns with unique values) of this table.
product_id is a foreign key (reference column) to Product table.
Each row of this table shows a sale on the product product_id in a certain year.
QUERY 1:
SELECT
product_id,
MIN(year) first_year,
quantity,
price
FROM
sales
GROUP BY
product_id
QUERY 2:
select
product_id,
year as first_year,
quantity,
price
from
Sales
where
(product_id, year) in (
select
product_id,
min(year)
from
Sales
group by
product_id
)
This is from a problem in LeetCode, my solution is Query#1 (it did not work), however, Query#2 seems to work but I cannot understand what the difference between the two is
New contributor
Muhammad Abubakar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.