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 trialJavid Abbasov
9,243 PointsPlease, HEEELP!!!!
I dont understand the meaning of codes after 4th line (starting from 5th). explain me, why do we need these codes?
select title,
min(score) as minimum_score,
max(score) as maximum_score,
avg(score) as average
from movies left outer join reviews
on movies.id = reviews.movie_id
group by movie_id
3 Answers
anil rahman
7,786 Pointsfrom movies left outer join reviews on movies.id = reviews.movie_id group by movie_id
This is saying we are selecting those colums above FROM the table movies. So title and score are from the table movies and because we said on line 5 from movies this is how it knows what we mean by select title. The next part is the left outer join which is one of a few joins you can use, you should look up joins because the different joins are important. But anyway, left outer join means i want to keep all my data from the movies table that ive just grabbed and dont want to lose any of that but now join on all the matching data from this other table reviews. So when you join on reviews you should get the same rows but where there is no matching data from reviews it will return null for that bit of data. After that is the on which basically says which two columns are my keys that i can identify a link from, so i primary key/id. The grouop by clause at the end just means group all my data selected by that column, so for example if you had 7 rows of data which had 2007 as the year you could group by year to show 1 row instead of 7 and could show a count which would be 7.
A X
12,842 PointsThis is my answer using code: JOINS are tough to understand! The different types of JOINS describe what data is joined to each other. People like to use ven diagrams for explaining this concept. See the ven diagrams at: http://blog.codinghorror.com/a-visual-explanation-of-sql-joins/
FROM movies -- we're looking at data inside the movies table
LEFT OUTER JOIN reviews --** see longer explanation
ON movies.id = reviews.movie_id --basically this is saying so what connects the two tables, in other words, what do the ----two tables share in terms of column names? It's their movie id
GROUP BY movie_id --it's a way of including all the data vs. just the first row in the tables
A X
12,842 PointsAnswer using pies for the JOIN part:
Let's say you have 2 delicious pies: a blueberry and an apple pie. You are more hungry than you've ever been in your life. So with the LEFT OUTER JOIN
you decide you first want the blueberry pie because it's literally on the left side of the case (same goes with a SQL query the LEFT is literally the data that's on the LEFT side). You eat the whole pie (in other words you get all the "data" from the pie on the left, which is blueberry). You finish the pie and you find out that a portion of the pie had delicious miracle berries ON it (which are a real thing that make sour things sweet). You ask, hoping against hope, that a portion of the apple pie that also has miracle berries ON it. As luck would have it, the apple pie has a small portion of it that has miracle berries ON it! (this would be the "data" that is also shared by the apple pie that you're JOINing with all the "data" from the blueberry pie and the miracle berries ON both is what they have in common so you can compare them)
So your pie "code" might look like:
select *,
from blueberry_pie left outer join apple_pie
on blueberry_pie.miracle_berries = apple_pie.miracle_berries
group by miracle_berries;