I have a problem with the following exercise query on this page http://sqlzoo.net/3.htm :
4d. List the 1978 films by order of cast list size.
I’m trying to do the query:
SELECT DISTINCT(m1.title), COUNT(c1.actorid)
FROM ((movie AS m1 JOIN casting AS c1 ON m1.id=c1.movieid) JOIN actor AS a1 ON a1.id=c1.actorid)
WHERE m1.yr=1978
GROUP BY m1.title
ORDER BY COUNT(c1.actorid) DESC
But it doesn’t give the right answer, and I don’t know why. Am I wrong?
This should do it:
As you don’t need any information from the actors, you don’t need to include that table in the join.
Btw: your usage of distinct shows two misunderstandings:
distinctis not a function. It always operates on all columns of the resultgroup bythere is no need to also to a distinct (as the group by will already return only the distinct values of the grouped columns)