Here’s the problem statement
Write a solution to select the product id, year, quantity, and price for the first year of every product sold.
Input:
Sales table:
+---------+------------+------+----------+-------+
| sale_id | product_id | year | quantity | price |
+---------+------------+------+----------+-------+
| 1 | 100 | 2008 | 10 | 5000 |
| 2 | 100 | 2009 | 12 | 5000 |
| 7 | 200 | 2011 | 15 | 9000 |
+---------+------------+------+----------+-------+
Product table:
+------------+--------------+
| product_id | product_name |
+------------+--------------+
| 100 | Nokia |
| 200 | Apple |
| 300 | Samsung |
+------------+--------------+
Output:
+------------+------------+----------+-------+
| product_id | first_year | quantity | price |
+------------+------------+----------+-------+
| 100 | 2008 | 10 | 5000 |
| 200 | 2011 | 15 | 9000 |
+------------+------------+----------+-------+
My solution
select product_id, min(year) as first_year, quantity, price from Sales
GROUP BY product_id;
How is this solution incomplete and why does this query pass 1 test case and fails the rest?
2
Answers
Here is the solution:
Well first of all, the query you just posted is not correct. Because here in this query:
You are trying to print out quantity and price after applying group by which is not possible. Because this is not how group by works, It is gonna split your whole table into smaller iterable groups of rows based on some common factor (which here in your case is product_id) It will make small groups of rows having same product_id. And it will apply aggregation over those only but we know aggregation returns a single value and the other columns you are trying to print can not be printed because the are supposed to have multiple values which again can not be printed because one cell contains a single value.(1st Nomal Form).
And comming back to main topic how the hell was it gonna be solved so here is the actual query that may solve the problem:
I hope it helps you out, otherwise you can ask me to explain it.