less than 1 minute read

Problem

problem-1070

Query

SELECT
    s2.product_id ,
    s1.first_year,
    s2.quantity,
    s2.price
FROM
    (
        SELECT MIN(year) AS first_year, product_id
        FROM Sales
        GROUP BY product_id
    ) AS s1,
    Sales AS s2
WHERE
    s1.product_id = s2.product_id AND s1.first_year = s2.year

Editorial Solution

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

Tags:

Updated: