DEV Community

Adam
Adam

Posted on

SQL Table Relationships

Joins is where you execute a query to retrieve table by combining two or more tables based on relationships of column ids.

There are multiple joins that you can achieve when retrieving data from your database

INNER JOIN

an Inner join is where you joining two tables and that record will appear if there is a matching records between two tables based on a condition if two columns have matching data.

SELECT column1, column2, ... FROM tbl_name_1
INNER JOIN tbl_name_2
ON tbl_name_1.matching_column = tbl_name_2.matching_column
LEFT JOIN

a Left join will show all rows from left side, and any matching results, it will be displayed on the right side of your result list. If there is no match in a row, it will return NULL. In this case, the left table in your join takes precedence over the right join, so make sure which table you are trying to select when finding matches in your data.

Left join is most used when trying to find matches

SELECT column1, column2, ... FROM tbl_name_1
LEFT JOIN tbl_name_2
ON tbl_name_1.matching_column = tbl_name_2.matching_column
RIGHT JOIN

A right join is where all records show on your right table, any matching records that you are trying to compare will show on the left side, if there is no match on a particular record, it will display NULL on the left side

SELECT column1, column2, ... FROM tbl_name_1
RIGHT JOIN tbl_name_2
ON tbl_name_1.matching_column = tbl_name_2.matching_column
FULL JOIN

A full join will return all records when there is a match on the left or right side. This is shows all data whether there is a match or not.

SELECT column1, column2, ... FROM tbl_name_1
FULL JOIN tbl_name_2
ON tbl_name_1.matching_column = tbl_name_2.matching_column
SELF JOIN

Top comments (0)