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 SQL Reporting by Example Day 1: Joining Tables 7th Grade Science

Hi. I did a totally different query, but generated the same result. Is there a preference for either method?

As Title but just out of curiosity, is there a preference?

'' SELECT DISTINCT TEACHERS.ID, FIRST_NAME, LAST_NAME FROM TEACHERS JOIN CLASSES ON CLASSES.TEACHER_ID = TEACHERS.ID WHERE SUBJECT_ID =21; ''

1 Answer

Gergely Bocz
Gergely Bocz
14,218 Points

Hi Jordan!

Your results are identical to the teachers because of a couple of reasons:

First of all, the Subject_id is separate for grades aswell, not only for subjects! This means, that if somebody from the 6th grade learns science, the corresponding subject_id (27) will be different from a 7th grader's subject_id(21) for example. You can check out the results yourself with this query:

SELECT DISTINCT * FROM SUBJECTS
JOIN CLASSES ON SUBJECTS.ID = CLASSES.SUBJECT_ID
WHERE SUBJECTS.NAME = "Science"

This means, that you specifying the subject_id is equal to specifying the subject name and the grade, just like the teacher did! However other databases aren't necessarily going to be structured like that, so you have to make a call based on the current database you are writing a query for!

I would also like to add, that while in such a small database you can easily search the subject_id you are looking for manually - which is what i assume you did in this case -, but in a larger database it may prove difficult, so i recommend joining the Classes and Subjects tables with a Where clause and specifying the subject's name, just like in the video.

I hope my explanation helps, if you have any more questions, feel free to ask!

All the best, Gergő