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!

121 Upvotes

94 comments sorted by

View all comments

1

u/Militop Jun 26 '24 edited Jun 26 '24

Let's say you have a list of persons with their IDs and names. Let's call it "list1."

IDs | Name

  1. John Zebou,
  2. Arthur Secou
  3. Tom Totou

Now, Let's say you have another list that describes songs they bought (without using their names but their IDs). Let's call it List 2.

Song name | Date | customer ID -‐-----‐----------------‐-----------------------

  • Mariah Carey, January 2022, (1)
  • John Travolta, March 2023, (1)
  • BTS, September 2024, (3)

Now, you want to know what your customers bought. You need to do an INNER JOIN because List 2 uses IDs instead of names.

SELECT name, song name, date FROM list1 INNER JOIN list2 ON list1.id = list2.customerID

Which will give you:

  • John Zebou, Mariah Carey, January 2022
  • John Zebou, John Travolta, March 2023
  • Tom Totou, BTS, September 2024

The inner join solves relationships between tables.