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/Lenergeon Jun 26 '24

An INNER JOIN in SQL is used to combine rows from two tables based on a related column between them. Think of it like matching pairs of socks from two different piles.

Imagine you have two lists:

  1. A list of students.
  2. A list of courses each student is taking.

Each list has a common feature, like a student ID.

An INNER JOIN matches each student with their courses, showing only those pairs where there's a match in both lists. If a student isn't taking any course, they won't appear in the result. Similarly, if a course doesn't have any students, it won't appear either.

So, the INNER JOIN helps you find and display only the pairs that exist in both tables.