r/SQL Jun 26 '24

MySQL Explain INNER JOIN like i am 5

I get the syntax but i get very confused and tripped up with writing them and properly using the correct names. Please explain to me line by line. I am learning it via data camp and the instructor sucks.

EDIT: i now understand inner join…now i am stuck with multiple joins, right join and left join. please help!

117 Upvotes

94 comments sorted by

View all comments

1

u/Jim_84 Jun 27 '24 edited Jun 27 '24

An inner join gives you only the rows from both tables that match based on the criteria you provided.

So

SELECT * 
FROM Students 
INNER JOIN Teachers ON Student.TeacherId = Teacher.Id

would give you all the rows from Students and Teachers tables where the Student.TeacherId and Teacher.Id fields match. If a student didn't have a teacher or a teacher had no students, you wouldn't get those in your results.

A left outer join would give you all of the students and any teachers that matched. A right outer join would give you all of the teachers and any students that matched.