Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6326959
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T17:11:21+00:00 2026-05-24T17:11:21+00:00

I am doing some SQL exercise and having this problem, this query below gives

  • 0

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T17:11:23+00:00Added an answer on May 24, 2026 at 5:11 pm

    The question asks

    Which were the busiest years?

    … plural. So, what were his top 5 years?

    select top 5
     m.yr
    ,count(*)
    from actor as a
        join casting as c
            join movie as m
            on m.movieid = c.movieid
        on c.actorid = a.actorid
    where a.name = 'John Travolta'
    group by
     m.yr
    order by
     count(*) desc
    

    However, the second part of the question specifies that you should

    Show the number of movies he made for each year.

    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.

    declare @year table
    (
        [yr] int
    )
    declare @currentYear int = datepart(year,getdate())
    while @currentYear >= 1954 begin -- Travolta was born in 1954!
    
        insert @year values (@currentYear)
        set @currentYear -= 1
    
    end
    
    select
     y.yr
    ,count(m.movieid)
    from @year y
        left join movies as m
            join casting as c
                join actor as a
                on a.actorid = c.actorid
                and a.name = 'John Travolta'
            on c.movieid = m.movieid
        on m.yr = y.yr
    group by
     y.yr
    order by
    ,count(m.movieid) desc
    

    [Edit: based on comments] And a final query to return all years whose count matches the highest of any year.

    ;with TravoltaMovies as
    (
        select
         m.yr
        ,count(*) as [Count]
        from actor as a
            join casting as c
                join movie as m
                on m.movieid = c.movieid
            on c.actorid = a.actorid
        where a.name = 'John Travolta'
        group by m.yr
    )
    select
    *
    from TravoltaMovies as tm
    where tm.[Count] = (select max([Count]) from TravoltaMovies)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm doing some recon work and having to dig through a few hundred SQL
I ran into a weird situation today doing some one-time sql code. This nested
Using SQL Server 2005. This is something I've noticed while doing some performance analysis.
I want to know how to retrieve results in a SQL query doing some
im doing some practice sql problems one of the questions is: Prepare a shipping
I am doing some T-SQL programming and I have some Views defines on my
I was doing some testing and straight LINQ-to-SQL queries run at least 80% faster
Does SQL Server 2005 maintain built-in, queryable, row-level last-modified timestamp metadata? I'm doing some
I am doing some tests using the SQL 2005 profiler. I have a stored
Hello I'm doing some data conversion from PostgreSQL to Microsoft SQL Server. So far

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.