I was trying to solve problem 15 under http://sqlzoo.net/wiki/More_JOIN_operations
I don't understand why my query is wrong, even though my output is like it's supposed to be.
Here's my query:
SELECT movie.title, COUNT(actorid)
FROM movie JOIN casting on movie.id=movieid
WHERE yr='1978'
GROUP BY casting.movieid
ORDER BY COUNT(casting.actorid) DESC
And the official answer:
SELECT title, COUNT(actorid)
FROM casting,movie
WHERE yr=1978
AND movieid=movie.id
GROUP BY title
ORDER BY 2 DESC
If I just change the ORDER BY
in my query from ORDER BY COUNT(casting.actorid) DESC
to ORDER BY 2 DESC
the answer is accepted (correct). Any reason for this?
All three of these should be accepted:
and:
and:
Kudos for using proper explicit
JOIN
syntax. Simple rule: do not use commas in theFROM
clause.As a note: I think the use of
2
might be removed from some future version of the ANSI-compliant databases.