Databases SQL Reporting by Example Day 1: Joining Tables 7th Grade Science

Harris Handoko
Harris Handoko
3,393 Points

Question on order of joining tables

I keep getting confused which table to start off, which table to join next. I want to develop some kind of a simple rule of thumb. Based on these exercises, I seem to conclude that when it comes to joining tables, we should start off our SELECT query with the table that contains our column of interest (WHO/WHICH). Then we will join the table that expresses the first table's relationship (HAS/DOES) with the third table that provides the condition (THIS/THAT).

-- Which teacher teaches 7th grade science? (Who does (or is associated with) that?)
SELECT T.ID, T.FIRST_NAME, T.LAST_NAME, S.NAME AS SUBJECT_NAME, S.GRADE 
  FROM TEACHERS AS T
  JOIN CLASSES AS C ON T.ID = C.TEACHER_ID
  JOIN SUBJECTS AS S ON C.SUBJECT_ID = S.ID
)

Is this a reliable rule of thumb? Or does it even matter?

1 Answer

ivana kantnerova
ivana kantnerova
12,494 Points

i think, It does not matter only the set of attributes that are in the relationship is created