DEV Community

Natalie Taktachev
Natalie Taktachev

Posted on

SQL Joins - Inner Join vs Outer Join

When storing information in a database, sometimes it will be necessary to link two tables that store information that is related.

There are multiple types of joins in SQL, but the main two categories are INNER JOIN and OUTER JOIN.

INNER JOIN:
In the simplest way, inner joins will only keep the information from the tables that is related. If you picture a Venn diagram, the inner join will only keep the information that is inside of the diagram.

The syntax for this is:

SELECT * FROM table1
JOIN table2
ON relation;

OUTER JOIN:
Outer joins will link the two tables, while also keeping unrelated data. There are three main types of outer joins in SQL: LEFT JOIN, RIGHT JOIN, and FULL JOIN.

LEFT JOIN will keep the unrelated data from the left (first) table. Picturing a Venn diagram, this join will keep the data is that is in the overlapping part of the diagram, as well as the data in the outside left circle. This is most commonly used.
The syntax for a LEFT JOIN is:

SELECT columns
FROM table1
LEFT JOIN table2
ON relation;

RIGHT JOIN will link the related data of the two tables, while keeping the unrelated data of table two (right table). The syntax for a RIGHT JOIN is the same as LEFT JOIN, but using the word 'right' instead.

A FULL JOIN will join the related information of two tables, while keeping the unrelated information.

The syntax for a FULL JOIN is:

SELECT columns
FROM table1
FULL JOIN table2
ON relation;

Discussion (0)