Table relationships and joins are fundamental concepts in relational databases. They allow you to combine data from multiple tables based on their logical relationships. The most common types of joins include INNER JOIN, LEFT JOIN, RIGHT JOIN, FULL JOIN, SELF JOIN, and CROSS JOIN.

Table of Contents
1. INNER JOIN
Definition:
The INNER JOIN
keyword retrieves records with matching values from both tables, excluding rows that do not have a corresponding match.
Syntax:
SELECT columns
FROM table1
INNER JOIN table2
ON table1.common_field = table2.common_field;
Example: Using two tables, Employees and Departments:
Employees Table:
EmployeeID | EmployeeName | DepartmentID |
1 | Alice | 1 |
2 | Bob | 2 |
3 | Charlie | 3 |
Departments Table:
DepartmentID | DepartmentName |
1 | HR |
2 | IT |
4 | Marketing |
Query:
SELECT Employees.EmployeeName, Departments.DepartmentName
FROM Employees
INNER JOIN Departments ON Employees.DepartmentID = Departments.DepartmentID;
Output:
EmployeeName | DepartmentName |
Alice | HR |
Bob | IT |
2. LEFT JOIN
Definition:
The LEFT JOIN
keyword returns all records from the left table, along with the corresponding matched records from the right table. When no match is found, the result includes NULL values for the columns from the right table.
Syntax:
SELECT columns
FROM table1
LEFT JOIN table2
ON table1.common_field = table2.common_field;
Example: Using the same Employees and Departments tables:
Query:
SELECT Employees.EmployeeName, Departments.DepartmentName
FROM Employees
LEFT JOIN Departments ON Employees.DepartmentID = Departments.DepartmentID;
Output:
EmployeeName | DepartmentName |
Alice | HR |
Bob | IT |
Charlie | NULL |
3. RIGHT JOIN
Definition:
The RIGHT JOIN
keyword retrieves all records from the right table and the relevant matched records from the left table. If no match is found, NULL values are inserted for the left table’s columns.
Syntax:
SELECT columns
FROM table1
RIGHT JOIN table2
ON table1.common_field = table2.common_field;
Example: Using the same Employees and Departments tables:
Query:
SELECT Employees.EmployeeName, Departments.DepartmentName
FROM Employees
RIGHT JOIN Departments ON Employees.DepartmentID = Departments.DepartmentID;
Output:
EmployeeName | DepartmentName |
Alice | HR |
Bob | IT |
NULL | Marketing |
4. FULL JOIN
Definition:
The FULL JOIN
keyword returns all records when there is a match in either the left or right table, and it fills in NULL values for rows without a match from either table.
Syntax:
SELECT columns
FROM table1
FULL JOIN table2
ON table1.common_field = table2.common_field;
Example: Using the same Employees and Departments tables:
Query:
SELECT Employees.EmployeeName, Departments.DepartmentName
FROM Employees
FULL JOIN Departments ON Employees.DepartmentID = Departments.DepartmentID;
Output:
EmployeeName | DepartmentName |
Alice | HR |
Bob | IT |
Charlie | NULL |
NULL | Marketing |
5. SELF JOIN
Definition:
A SELF JOIN
is a type of join where a table is joined with itself, allowing for comparisons between rows within the same table.
Syntax:
SELECT a.columns, b.columns
FROM table a, table b
WHERE condition;
Example: Consider an Employees table with a ManagerID:
Employees Table:
EmployeeID | EmployeeName | ManagerID |
1 | Alice | NULL |
2 | Bob | 1 |
3 | Charlie | 1 |
Query:
SELECT a.EmployeeName AS Employee, b.EmployeeName AS Manager
FROM Employees a
LEFT JOIN Employees b ON a.ManagerID = b.EmployeeID;
Output:
6. CROSS JOIN
Definition:
A CROSS JOIN
produces the Cartesian product of two tables, where each row from the first table is combined with every row from the second table.
Syntax:
SELECT columns
FROM table1
CROSS JOIN table2;
Example: Using the same Employees and Departments tables:
Query:
SELECT Employees.EmployeeName, Departments.DepartmentName
FROM Employees
CROSS JOIN Departments;
Output:
EmployeeName | DepartmentName |
Alice | HR |
Alice | IT |
Alice | Marketing |
Bob | HR |
Bob | IT |
Bob | Marketing |
Charlie | HR |
Charlie | IT |
Charlie | Marketing |
Conclusion
Understanding the joins is crucial for working with relational databases. Combining data from multiple tables allows for complex queries and deeper insights into data relationships.
Most Commented