Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Databases Reporting with SQL Aggregate and Numeric Functions Practice Session

MAX/MIN Question

I'm doing the final practice for this section and it asked for the Max & Min for each movie. I got those but how do I get the movie_id for both Max & Min? I added movie_id to my query and it only showed one ID but Max & Min are 2 different IDs. Thanks!!

2 Answers

Jonathan Grieve
MOD
Jonathan Grieve
Treehouse Moderator 91,253 Points

max() and min() are SQL methods that manipulate data that already exists.

What's happening is that the movie table is a foreign key table, which means it has a "many" relationship. The movie_id is a foreign key column so it can appear more than once. So you should only need to select the movie_id once in your query in order to get the min() and max() data

My apologies as I may have asked the question wrong. How do I get the movie_id to show with the Min() and the Max()? I did get the Min () and Max() data but I could not get the ID for each. They are two different IDs but it only showed one.

Dustin Honeck
Dustin Honeck
12,504 Points

I think you were missing the GROUP BY keyword. If you don't include it, your query will return the MIN and MAX rating from all of the rows but will not show the MIN and MAX for each movie_id.

-- I am guessing you entered something like this
SELECT movie_id, MIN(rating), MAX(rating) FROM reviews;

-- But should have entered something like 
SELECT movie_id, MIN(rating), MAX(rating) FROM reviews GROUP BY movie_id;