Why can't I get the right answer in this SQLzoo tutorial?
Question
Problem 2b goes as follows:
2b. For each subject show the first year that the prize was awarded.
nobel(yr, subject, winner)
My solution was this:
SELECT DISTINCT subject, yr
FROM nobel
ORDER BY yr ASC;
Why isn't this working?
Solution
Your answer gets a row for every distinct combination of subject and year.
The correct answer GROUPS BY the subject, and gets the MIN year per subject.
Enough of a clue?
OTHER TIPS
You could do it a different way without using group by or min
select distinct subject, yr from nobel x
where yr <= all
(select yr from nobel y
where y.subject = x.subject)
but its definitely more work.
SELECT subject, MIN(yr)
FROM nobel
GROUP BY subject;
Yay!
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow