SQL joins
Combining multiple tables is where the real power of SQL lives. SQL can combine multiple tables on criteria that match and report them as one result set. That is where a JOIN clause comes in.
A JOIN clause is used to combine rows from two or more tables, based on a related column between them. Here are several Venn diagrams showing the visual representation of the different Joins available:
While the majority of your joins will tend to be INNER
or LEFT
join statements, we will go over all the joins available to you:
#
INNER JOINThe INNER JOIN
statement in SQL gets only the records where a match occurs on both tables. The result set looks like this:
The INNER JOIN
statement follows this syntax:
#
LEFT JOINThe LEFT JOIN
statement in SQL gets all the records from the left table and only records from the right table where a match occurs. The result set looks like this:
The LEFT JOIN
statement follows this syntax:
#
RIGHT JOINThe RIGHT JOIN
statement in SQL gets all the records from the right table and only records from the left table where a match occurs. The result set looks like this:
The RIGHT JOIN
statement follows this syntax:
NOTE: Although Right Join is available to you, industry standard/best practice is to use a Left Join instead.
#
FULL OUTER JOINThe FULL OUTER JOIN
statement in SQL gets all the records from both the left and right tables where a match occurs in either table. The result set looks like this:
The FULL OUTER JOIN
statement follows this syntax:
NOTE: Although Full Outer Join is available to you, there are very few cases where this Join will be required.
#
LEFT EXCLUDING JOINThe LEFT EXCLUDING JOIN
statement in SQL gets all the records from the left table where a match does not occur on the right table. The result set looks like this:
The LEFT EXCLUDING JOIN
statement follows this syntax:
#
RIGHT EXCLUDING JOINThe RIGHT EXCLUDING JOIN
statement in SQL gets all the records from the right table where a match does not occur on the left table. The result set looks like this:
The RIGHT EXCLUDING JOIN
statement follows this syntax:
NOTE: Although Right Excluding Join is available to you, industry standard/best practice is to use a Left Excluding Join instead.
#
FULL OUTER EXCLUDING JOINThe FULL OUTER EXCLUDING JOIN
statement in SQL gets all the records from both the left and right tables where a match does not occur in either table. The result set looks like this:
The FULL OUTER EXCLUDING JOIN
statement follows this syntax:
NOTE: Although Full Outer Excluding Join is available to you, there are very few cases where this Join will be required.
#
Takeaways- JOINS combine results from multiple tables based on relationships
- LEFT JOIN pulls all results from the left table and only matched results from the right table
- INNER JOIN only pulls results where matches are found on both left and right tables