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 trialAllan Oloo
8,054 PointsLEFT JOIN & RIGHT JOIN
This may sound like a stupid question, but I am curious to know what makes a table left or right? why not use a where caulse with the OR keyword to get data that matches regardless whether they exist or not? I don't know if my question made sense. But I am a little confused with those JOIN statements. Also how do you know which order to join tables when writing a query to select a specific data.
1 Answer
Steven Parker
231,268 PointsThe terms "LEFT
" and "RIGHT
" refer to which side of the JOIN
keyword gets priority.
A "LEFT JOIN
" will use all rows from the expression before the join, and and "RIGHT JOIN
" will use all rows of the table mentioned in the join itself.
Since you can choose either type to fit your needs, the main criteria for selecting the order of joins is to be sure that anything referenced in the "ON
" clause of the join has already been included.
Tom Crow
719 PointsTom Crow
719 PointsTo expand on Stevens great answer, choosing the table order is dependent on where the related data is.
So in challenge 3, the loans table contains relations to both the patrons and books tables, so it makes sense to bring that one in first. You can then join the books table in next, relating it to the loans table, and then the patrons table last, again relating it to the loans table.
We also know that we want to return a list of all the loans. So we can safely LEFT OUTER JOIN the loans table to the books table, as if a book hasnβt been loaned we donβt need to see it. We then also know if a book shows up in the loans table, someone must have loaned it, so we can safely LEFT OUTER JOIN the patrons table, as know all books that get pulled in during our first join have been loaned by someone.
Thatβs my understanding, please anyone correct me if Iβm wrong and Iβm sorry if that sounded a bit muddled. I donβt find joins simple or easy either lol.