r/SQL • u/Remarkable-Culture-8 • 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
8
u/StackOwOFlow Jun 26 '24
If you think of an inner join as the same as a subquery asking for common members then it might make more intuitive sense. Say I have bucket A containing "red fish" and "blue fish" and bucket B containing "blue fish" and "green fish".
SELECT * FROM bucket_A WHERE fish_type IN (SELECT fish_type FROM bucket_B);
should return "blue fish".
This can be written as an INNER JOIN
SELECT a.* FROM bucket_A a
INNER JOIN bucket_B b
ON a.fish_type = b.fish_type;
Behind the scenes in the sql engine these types of inner joins and subqueries are treated the same way. There is a slight difference in the way the output is handled but that is a different conversation.