In the nobel quiz, there is a question "Write the code which would show the year when neither a Physics or Chemistry award was given’.
My code is as following:
SELECT yr FROM nobel
WHERE subject NOT IN ('Chemistry','Physics')
The correct code should be:
SELECT yr FROM nobel
WHERE yr NOT IN (SELECT yr FROM nobel WHERE subject IN ('Physics', 'Chemistry'))
I am wondering why my code is incorrect. Why do we have to use the subquery for this question?
Thanks everyone!
2
Answers
Say you have this data:
In this case, your code will still match the
Biology
row and return2023
. However, there is also a row forChemistry
in this year, and this violates the requirement.Your original query checks for years where the subject is not Chemistry or Physics, which means it could include years where only one of them (either Chemistry or Physics) was not awarded, but the other one was. It doesn’t ensure that neither Physics nor Chemistry was awarded in that year.