Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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 90,825 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.

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;