I am doing some SQL exercise and having this problem, this query below gives me a ‘half correct’ result because I only want the row(s) with the most title to be displayed, this query is displaying all records. Can someone help? Thanks.
Question:
Which were the busiest years for ‘John Travolta’. Show the number of movies he made for each year.
Tables:
movie (id, title, yr, score, votes, director)actor (id, name)casting (movieid, actorid, ord)
Query:
select yr, max(title)
from
(
select yr, count(title) title from movie
join casting
on (movie.id=casting.movieid)
join actor
on (casting.actorid=actor.id)
where actor.name="John Travolta"
group by yr Asc
) a
The question asks
… plural. So, what were his top 5 years?
However, the second part of the question specifies that you should
So far our query doesn’t account for years in which John made no movies… so, this might be where your half correct comes into play. That said, you may want to create a table variable filled with year values from 1954 through the current year… and left join off of that.
[Edit: based on comments] And a final query to return all years whose count matches the highest of any year.