I was feeling good about the speed that I was progressing through the early SQLzoo SQL tutorials. Then I hit question eight in the “SELECT FROM world” tutorial, and couldn’t work out what response the question was seeking. It seems from the various help threads on StackOverflow and Reddit that I am not the only one who has had this issue.
To my mind, the phrasing of the question implies that there is an
XOR feature in SQL, as there is in many other languages. So first I tried using the same format as the
OR-based answer to the previous question, but changing the operator:
SELECT name, population, area FROM world WHERE area > 3000000 XOR population > 250000000
This didn’t work. So then I think “maybe there is no
XOR operator in SQL and I have to do this manually”. So I try:
SELECT name, area, population FROM world WHERE (area > 3000000 AND population <= 250000000) OR (area <= 3000000 AND population > 250000000)
This gives the following error:
Wrong answer. Some of the data is incorrect.
But the data are not incorrect. Only after some fretting did I realise that SQLzoo wants you to
SELECT the population before you call the area. Hence the correct solution is:
SELECT name, population, area FROM world WHERE (area > 3000000 AND population <= 250000000) OR (area <= 3000000 AND population > 250000000)
This got me the “Correct answer” result, and the weird happy-face emoji. Onwards!