I am wondering how to differentiate all these different joins ...
SQL JOINS difference:
Very simple to remember :
INNER JOIN
only show records common to both tables.
OUTER JOIN
all the content of the both tables are merged together either they are matched or not.
LEFT JOIN
is same as LEFT OUTER JOIN
- (Select records from the first (left-most) table with matching right table records.)
RIGHT JOIN
is same as RIGHT OUTER JOIN
- (Select records from the second (right-most) table with matching left table records.)
Inner join: Only show rows, when has it data from both of the tables.
Outer join: (left/right): Show the all result from the left / right table with the paired row(s), if it exists or not.
Making it more visible might help. One example:
Table 1:
ID_STUDENT STUDENT_NAME
1 Raony
2 Diogo
3 Eduardo
4 Luiz
Table 2:
ID_STUDENT LOCKER
3 l1
4 l2
5 l3
What I get when I do:
-Inner join of Table 1 and Table 2:
- Inner join returns both tables merged only when the key
(ID_STUDENT) exists in both tables
ID_STUDENT STUDENT_NAME LOCKER
3 Eduardo l1
4 Luiz l2
-Left join of Table 1 and Table 2:
- Left join merges both tables with all records form table 1, in
other words, there might be non-populated fields from table 2
ID_ESTUDANTE NOME_ESTUDANTE LOCKER
1 Raony -
2 Diogo -
3 Eduardo l1
4 Luiz l2
-Right join of table 1 and table 2:
- Right join merges both tables with all records from table 2, in
other words, there might be non-populated fields from table 1
ID_STUDENT STUDENT_NAME LOCKER
3 Eduardo l1
4 Luiz l2
5 - l3
-Outter join of table 1 and table 2:
- Returns all records from both tables, in other words, there
might be non-populated fields either from table 1 or 2.
ID_STUDENT STUDENT_NAME LOCKER
1 Raony -
2 Diogo -
3 Eduardo l1
4 Luiz l2
5 - l3