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

Kevin S
seal-mask
.a{fill-rule:evenodd;}techdegree
Kevin S
Data Analysis Techdegree Student 15,862 Points

How to Take SQL INNER JOIN Basic Question One Step Further

I am working on this question from the SQL Playground Joins Review...

-- Use a JOIN to select all patrons with outstanding books. Select their first name and email address.

I have successfully answered the question with the following Code

SELECT * from patrons INNER JOIN loans ON patrons.id = loans.patron_id WHERE loans.returned_on IS NULL AND loans.return_by < DATE("now");

how can I take this one step further by also selecting the loaned_on and return_by columns from the loans table? This would allow me to easily check my answer without having to do separate lookups for each entry to check if i am right.

Thanks all.

3 Answers

Dave StSomeWhere
Dave StSomeWhere
19,870 Points

Instead of SELECT * FROM patrons which selects all columns from patrons you'll want to select the individual columns that you desire. Also, you'll use an alias to specify the file names.

Something like:

SELECT a.col1, a.col2, b.colx, b.coly FROM patrons INNER JOIN loans as b .... (you can still use a.* to select all columns).

Go to SQL Fiddle and experiment.

Also, it would probably be worth your time to load MySQL on your local machine. There are many options depending on your OS.

I used the LEFT OUTER JOIN with patron as the left sided table. A rough Venn diagram can show clearly that table patrons and books are completely distinct from each other but both join the loans table on patron-id on the left side and book-id on the right side. In addition, loans table has its own independent columns.

[SELECT first_name, email, return_by, return_on, loaned_on FROM patrons LEFT OUTER JOIN loans ON patrons.id = loans.patron_id WHERE loans.returned_on IS NULL AND loans.return_by < DATE("now");]

I added the third (return_by) and fourth (return_on) columns simply to prove whether my answer made sense. I could not use DISTINCT because at any one time a single patron can have more that one outstanding books and yet I need to identify each of outstanding books individually. The correct columns to select therefore are according to the question, first name and email but I even added the column issued_on and clearly the returned on column is all blank because these books are still outstanding to date. I avoided aliasing unnecessarily as this would rather appear cluttering my code although in complex queries this would have been necessary.

Err: replace issued on with loaned_on