Join in Relational Database and its various types

Join in Relational Database

Join in Relational Database is an important mechanism to show data from two or multiple tables. Depending upon the requirement different types of join involves combining rows from two or more tables based on a related column between them. Here’s an overview of different types of joins:

Different types of Join in Relational Database

  • Inner Join: Returns only the rows that have matching values in both tables.
  • Left (Outer) Join: Returns all rows from the left table, and the matched rows from the right table. If no match, the result is NULL on the side of the right table.
  • Right (Outer) Join: Returns all rows from the right table, and the matched rows from the left table. If no match, the result is NULL on the side of the left table.
  • Full (Outer) Join: Returns all rows when there is a match in either left or right table. If no match, the result is NULL from the table that does not have a match.
  • Cross Join: Returns the Cartesian product of the two tables, i.e., it returns all possible combinations of rows.

Here are some SQL examples:

Inner Join in Relational Database

SELECT A.column1, B.column2
FROM TableA A
INNER JOIN TableB B
ON A.common_column = B.common_column;

Left (Outer) Join in Relational Database

SELECT A.column1, B.column2
FROM TableA A
LEFT JOIN TableB B
ON A.common_column = B.common_column;

Right (Outer) Join in Relational Database

SELECT A.column1, B.column2
FROM TableA A
RIGHT JOIN TableB B
ON A.common_column = B.common_column;

Cross Join in Relational Database

SELECT A.column1, B.column2
FROM TableA A
CROSS JOIN TableB B;

Join Example Scenario in Relational Database

Let’s consider two tables: Employees and Departments.

Employees Table:

EmpIDEmpNameDeptId
1John10
2Jane20
3Bob30

Departments Table:

DeptIdDeptName
10Sales
20Marketing
40IT

Inner Join Example in Relational Database

SELECT Employees.EmpName, Departments.DeptName
FROM Employees
INNER JOIN Departments
ON Employees.DeptID = Departments.DeptID;

Result:

EmpNameDeptName
JohnSales
JaneMarketing

Left Join Example in Relational Database

SELECT Employees.EmpName, Departments.DeptName
FROM Employees
LEFT JOIN Departments
ON Employees.DeptID = Departments.DeptID;

Result:

EmpNameDeptName
JohnSales
JaneMarketing
BobNULL

These join operations are fundamental to querying relational databases and allow for flexible data retrieval across multiple related tables.

Right Join Example in Relational Database

The Right Join retrieves all records from the right table (Departments), and the matched records from the left table (Employees). The result is NULL from the left side, when there is no match.

SELECT Employees.EmpName, Departments.DeptName
FROM Employees
RIGHT JOIN Departments
ON Employees.DeptID = Departments.DeptID;

Result:

EmpNameDeptName
JohnSales
JoeMarketing
NULLIT

In this result, you can see that even though there is no employee associated with the IT department (DeptID 40), the department still appears in the results, with NULL values for the employee columns.

Full Outer Join Example in Relational Database

The Full Outer Join combines the results of both Left Join and Right Join. It retrieves all records when there is a match in either left (Employees) or right table (Departments). If there is no match, the result is NULL from the table without the match.

SELECT Employees.EmpName, Departments.DeptName
FROM Employees
FULL OUTER JOIN Departments
ON Employees.DeptID = Departments.DeptID;

Result:

EmpNameDeptName
JohnSale
JaneMarketing
BobNULL
NULLIT

Self Join in Relational Database

A Self Join is a regular join, but the table is joined with itself. This can be useful when you want to compare rows within the same table.

For example, suppose we have an Employees table with a column ManagerID that refers to the EmpID of the manager:

Employees Table:

EmpIDEmpNameDeptIdManagerID
1John10NULL
2Jane201
3Bob301
4Alice102

To list employees along with their manager’s name, you could write:

SELECT e1.EmpName AS Employee, e2.EmpName AS Manager
FROM Employees e1
LEFT JOIN Employees e2
ON e1.ManagerID = e2.EmpID;

Result:

EmployeeManager
JohnNULL
JaneJohn
BobJohn
AliceJane

Cross Join Example in Relational Database

The Cross Join produces the Cartesian product of the two tables involved. This means that each row from the first table is combined with all rows in the second table. It does not require any condition for joining.

SELECT Employees.EmpName, Departments.DeptName
FROM Employees
CROSS JOIN Departments;

Result:

EmpNameDeptName
JohnSales
JohnMarketing
JohnIT
JaneSales
JaneMarketing
JaneIT
BobSales
BobMarketing
BobIT

This type of join can generate a large number of rows if both tables have many rows.

Using Aliases in Joins in Relational Database

In SQL queries, especially those with joins, aliases can simplify your code and make it more readable. Aliases are temporary names given to tables or columns for the duration of a query.

Example with aliases:

SELECT e.EmpName, d.DeptName
FROM Employees e
INNER JOIN Departments d
ON e.DeptID = d.DeptID;

In this example, e and d are aliases for Employees and Departments, respectively.

Combining Multiple Joins in Relational Database

You can combine multiple joins in a single query to retrieve data from more than two tables. Here’s an example that combines inner and left joins:

Assume we have an additional Salaries table:

Salaries Table:

EmpIDSalary
17000
28000
46000

To retrieve employee names, department names, and their salaries, you can write:

SELECT e.EmpName, d.DeptName, s.Salary
FROM Employees e
INNER JOIN Departments d
ON e.DeptID = d.DeptID
LEFT JOIN Salaries s
ON e.EmpID = s.EmpID;

Result:

EmpNameDeptNameSalary
JohnSales7000
JaneMarketing8000
BobNULLNULL
AliceNULL600

Conclusion

Understanding the various types of joins and how to use them effectively is crucial for querying relational databases. Joins allow you to combine and manipulate data from multiple tables to gain comprehensive insights. Always choose the appropriate type of join based on your data retrieval requirements to ensure accurate and efficient results.

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *