The SQL language allows tables to be joined, but sometimes these commands are forgotten and/or we get confused about the real purpose of each one.
Let’s take a look at the image below, representing the various forms of JOINS:
INNER JOIN
Intersection between tables.
Example:
SELECT <colunas> FROM Table_A A INNER JOIN Table_B B ON A.Key = B.Key
RIGHT JOIN and LEFT JOIN
In addition to the intersection, it also searches for results that have no intersection. LEFT JOIN takes the results from the table on the left, plus the result of the intersection between the two tables, and RIGHT JOIN takes the values from the table on the right, plus the result of the intersection between the two tables.
Example:
SELECT <colunas> FROM Table_A A LEFT JOIN Table_B B ON A.Key = B.Key
SELECT <colunas> FROM Table_A A RIGHT JOIN Table_B B ON A.Key = B.Key
FULL OUTER JOIN
In addition to the intersection, it also does LEFT JOIN and RIGHT JOIN, i.e. it searches table A for results that do not have an intersection, searches table B for results that do not have an intersection, and finally searches for the results of the intersection.
Example:
SELECT <colunas> FROM Table_A A FULL OUTER JOIN Table_B B ON A.Key = B.Key