Updated March 31, 2023
Introduction to dbms join
dbms join is a very important concept when we want to fetch the data or record from two tables based on the common attribute of both the tables. Joins are divided into different types, and it is one of the most important concepts of relational databases. We have different types of joins: inner join, outer join, left join, right join, and many more. So when we want to fetch data from multiple tables and when we deal with so many tables based on the mapping joins is very helpful; it helps us to get the data or records from different tables. It is also one of the most asked interview topics. So, in short, we can define joins in a relational database as the Cartesian product of two tables; it consists of selection and product process to get the desired result from multiple tables. In the coming section of the tutorial, we will be going to see in more detail about the joins with each type detailed explanation.
Types of Join
dbms joins are broadly divided into mainly two types, and these types are further divided into subtypes. All of the types have their own significance, and we can use each of them in our application to get the desired result from the database tables. Hence it helps us to join multiple tables and get the result out of that based on the selection process; let’s get quickly start a discussion about the type of join we have seen below;
The broad view for different types of joins is a follows;
1) inner join
2) outer join
let’s discuss each of them in detail;
1) Inner Join: Inner join is the most common form of joins in dbms; this joins helps us to get the common result from both the tables. This uses a common attribute from both tables to map the data from each table. We can select specific fields or all the attributes of both fields to get the result. Based on the id provided commonly in both tables, it will return the match record from both the tables. Let take a look at the diagram to understand this Cartesian product from the tables see below;
Diagram:
Syntax:
SELECT your columns(s)
FROM table_one
INNER JOIN table_two
ON table_one.column_name = table_two.column_name;
As you can see from the above syntax, we are trying to make use of inner use; for that, we are using ‘inner join’ as the keyword to join two tables. Let’s take a closer look at the query, which will help you understand the query well;
Inner Join Query:
SELECT cus.CustomerName, cus.City, cus.Country, ord.OrderID, ord.EmployeeID
FROM Orders as ord
INNER JOIN Customers as cus ON ord.CustomerID = cus.CustomerID;
Output:
Now inner join can be further divided into two more subtypes which is mentioned in the below points; let’s take a detailed look at both of them see below;
i) Equi Join: This is one of the types of inner join itself, which makes use of equivalence condition and based in that it tries to fetch the record from both the tables. In this section, we will see a closer look at the syntax and query see below;
Equi Join Query:
SELECT cus.CustomerName, cus.City, cus.Country, ord.OrderID, ord.EmployeeID
FROM Orders as ord
INNER JOIN Customers as cus ON ord.CustomerID = cus.CustomerID;
Output:
ii) Natural Join: It is also one of the types of inner join which makes use of common attributes in both tables to return the result record; it does not depend on or use any comparison operator to get the result or join two tables. This can also be created using the ‘Natural join’ keyword while writing the query using natural join. Let’s take a closer look at the syntax and query for a better understanding of this join in detail see below;
Natural Join Query:
Select * from Orders Natural Join Customers;
Output:
2) Outer Join: Now, the second type of join we have is outer join, which is further divided into subcategories to get the desired results from the multiple tables; whether there is a match or not in both the tables depends on which type of outer join we are using. Let’s take a closer look at all the types available in it to see below;
i) Left-outer join: This is one of the most common type outer join we have. for this, we use the ‘left join’ keyword inside the query to get the result. But it does return the record differently; let understand this, This basically returns all the records from the first table, that is, the left table, with the matching record found in the right table or the second table. Let’s understand this by a simple diagram for better clarity see below;
Diagram:
Syntax:
SELECT cus.CustomerName, cus.City, cus.Country, ord.OrderID, ord.EmployeeID
FROM Orders as ord
LEFT OUTER JOIN Customers as cus ON ord.CustomerID = cus.CustomerID;
Output:
ii) Right outer join: This is one of the most common types of outer join we have. for this, we use the ‘right join’ keyword inside the query to get the result. But it does return the record differently; let understand this, This basically returns all the records from the second table, that is, the right table, with the matching record found in the left table or the first table. Let’s understand this by a simple diagram for better clarity see below;
Diagram:
syntax:
SELECT emp.FirstName, emp.LastName, emp.notes, ord.OrderID
FROM Orders as ord
RIGHT OUTER JOIN Employees as emp
ON ord.EmployeeID = emp.EmployeeID;
Output:
iii) Full outer join: As the name suggests, it returns all the records from both the tables, whether there is a match or not. That means it will be the Cartesian product for both the tables; let’s understand them by the use of the below diagram for better understanding see below;
Diagram:
Syntax:
SELECT emp.FirstName, emp.LastName, emp.notes, ord.OrderID
FROM Orders as ord
FULL OUTER JOIN Employees as emp
ON ord.EmployeeID = emp.EmployeeID;
Output:
Conclusion – dbms join
We have covered all the types of joins present in the dbms, which helps us get the record from multiple tables. Also, we have seen joins that return records from either of the tables match the other tables. This is easy to understand by the developers, readable and maintainable, with a lot of online support.
Recommended Articles
We hope that this EDUCBA information on “dbms join” was beneficial to you. You can view EDUCBA’s recommended articles for more information.