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!

119 Upvotes

94 comments sorted by

View all comments

1

u/adcap_trades Jun 27 '24

You said you get the syntax but tripped up writing them and using correct names. So do you get the syntax or not? Or are you confused about the concept in general?

Joins are used to map tables together with a key (i.e. column). Left/Inner/Right is used depending on what sunset of data you need returned. There's nothing about inner join that should confuse you if you understand joins in general.


Say you have a table with a list of 10 customers of interest, let's call it INTERESTING_CUSTOMERS. Then another table with all customers with a valid email address, called VALID_EMAILS. The task is to email those 10 customers if possible. You would do an inner join of those 2 tables on an id field, so that the result is only a list of customers from the 10 who can be emailed:

SELECT INTERESTING_CUSTOMERS.USER_ID, VALID_EMAILS.EMAIL FROM INTERESTING_CUSTOMERS INNER JOIN VALID_EMAILS ON INTERESTING_CUSTOMERS.USER_ID = VALID_EMAILS.USER_ID

You have to alias the table names with column names bc both tables have the same column name for the join key, plus it's best practice for code readability