In SQL, joins are used to combine rows from two or more tables based on a related column between them. Joins are essential in relational databases because they allow users to retrieve related data that is distributed across multiple tables.
There are several types of joins, each serving a different purpose depending on how you want to match rows between the tables. Below are the different types of joins in SQL:
INNER JOIN returns only the rows where there is a match in both tables. If there is no match, the row will not be included in the result.Syntax:
SELECT columns
FROM table1
INNER JOIN table2
ON table1.common_column = table2.common_column;
Example:
Suppose we have two tables, Employees and Departments:
Employees:
| EmployeeID | Name | DepartmentID |
|---|---|---|
| 1 | Alice | 101 |
| 2 | Bob | 102 |
| 3 | Charlie | 103 |
Departments:
| DepartmentID | DepartmentName |
|---|---|
| 101 | HR |
| 102 | IT |
| 104 | Marketing |
To get the names of employees along with their department names (only for employees who have a department):
SELECT Employees.Name, Departments.DepartmentName
FROM Employees
INNER JOIN Departments
ON Employees.DepartmentID = Departments.DepartmentID;
Result:
| Name | DepartmentName |
|---|---|
| Alice | HR |
| Bob | IT |
LEFT JOIN (or LEFT OUTER JOIN) returns all the rows from the left table and the matching rows from the right table. If there is no match, NULL values are returned for columns from the right table.Syntax:
SELECT columns
FROM table1
LEFT JOIN table2
ON table1.common_column = table2.common_column;
Example:
Using the same Employees and Departments tables, to get all employees, including those who don't belong to a department:
SELECT Employees.Name, Departments.DepartmentName
FROM Employees
LEFT JOIN Departments
ON Employees.DepartmentID = Departments.DepartmentID;
Result:
| Name | DepartmentName |
|---|---|
| Alice | HR |
| Bob | IT |
| Charlie | NULL |
In this case, Charlie doesn't have a matching department, so the DepartmentName is NULL.
RIGHT JOIN (or RIGHT OUTER JOIN) returns all the rows from the right table and the matching rows from the left table. If there is no match, NULL values are returned for columns from the left table.Syntax:
SELECT columns
FROM table1
RIGHT JOIN table2
ON table1.common_column = table2.common_column;
Example: To get all departments and their employees (including departments that don't have any employees):
SELECT Employees.Name, Departments.DepartmentName
FROM Employees
RIGHT JOIN Departments
ON Employees.DepartmentID = Departments.DepartmentID;
Result:
| Name | DepartmentName |
|---|---|
| Alice | HR |
| Bob | IT |
| NULL | Marketing |
In this case, the Marketing department has no employees, so the Name column is NULL.
FULL OUTER JOIN returns all rows from both the left and right tables. If there is no match, NULL values are returned for missing matches on either side.Syntax:
SELECT columns
FROM table1
FULL OUTER JOIN table2
ON table1.common_column = table2.common_column;
Example: To get all employees and all departments, whether or not an employee is assigned to a department and whether a department has employees:
SELECT Employees.Name, Departments.DepartmentName
FROM Employees
FULL OUTER JOIN Departments
ON Employees.DepartmentID = Departments.DepartmentID;
Result:
| Name | DepartmentName |
|---|---|
| Alice | HR |
| Bob | IT |
| Charlie | NULL |
| NULL | Marketing |
In this case, Charlie has no department, and the Marketing department has no employees, so both sides contain NULLs where no match is found.
CROSS JOIN returns the Cartesian product of both tables. This means that each row in the first table is combined with every row in the second table. It doesn't require a condition to join.CROSS JOIN when you want to get all possible combinations of rows between two tables.Syntax:
SELECT columns
FROM table1
CROSS JOIN table2;
Example:
To generate a list of all possible combinations between two tables, Employees and Departments:
SELECT Employees.Name, Departments.DepartmentName
FROM Employees
CROSS JOIN Departments;
Result:
| Name | DepartmentName |
|---|---|
| Alice | HR |
| Alice | IT |
| Alice | Marketing |
| Bob | HR |
| Bob | IT |
| Bob | Marketing |
| Charlie | HR |
| Charlie | IT |
| Charlie | Marketing |
This will produce a result where each employee is paired with every department, regardless of their actual department assignment.
Syntax:
SELECT A.column, B.column
FROM table A, table B
WHERE A.common_column = B.common_column;
Example:
Consider an Employees table where each employee may report to another employee:
| EmployeeID | Name | ManagerID |
|---|---|---|
| 1 | Alice | NULL |
| 2 | Bob | 1 |
| 3 | Charlie | 1 |
To get the names of employees and their managers:
SELECT E1.Name AS Employee, E2.Name AS Manager
FROM Employees E1
LEFT JOIN Employees E2
ON E1.ManagerID = E2.EmployeeID;
Result:
| Employee | Manager |
|---|---|
| Alice | NULL |
| Bob | Alice |
| Charlie | Alice |
In this example, E1 represents employees and E2 represents their managers. The result shows that Bob and Charlie report to Alice.
SQL joins are essential for retrieving related data from multiple tables in relational databases. Depending on your requirements, you can choose the appropriate type of join:
Understanding these different join types is fundamental to querying relational databases effectively and efficiently.
Open this section to load past papers