JOINS are used to combine rows from two or more tables, based on a related column between those tables. The following are the types of joins:
- INNER JOIN: This join returns those records which have matching values in both the tables.
- FULL JOIN: This join returns all those records which either have a match in the left or the right table.
- LEFT JOIN: This join returns records from the left table, and also those records which satisfy the condition from the right table.
- RIGHT JOIN: This join returns records from the right table, and also those records which satisfy the condition from the left table.
Refer to the image below.
INNER JOIN
Syntax:
SELECT column_name(s)
FROM table1
INNER JOIN table2 ON table1.column_name = table2.column_name;
FULL JOIN
Syntax:
SELECT column_name(s)
FROM table1
FULL OUTER JOIN table2 ON table1.column_name = table2.column_name;
LEFT JOIN
Syntax:
SELECT column_name(s)
FROM table1
LEFT JOIN table2 ON table1.column_name = table2.column_name;
RIGHT JOIN
Syntax:
SELECT column_name(s)
FROM table1
RIGHT JOIN table2 ON table1.column_name = table2.column_name;