Difference Between Right Join and Right Outer Join
Last Updated :
20 Sep, 2024
Joins in a Database (SQL) are mostly used for combining data or the rows of two or more table records that are based on the same or common attribute. There are various types of Joins like Right Join, Left Join, Full Join, etc. Each join has its own syntax and data-returning capability. In this article, we will see the information about Right Join and Right Outer Join along with the example, also we will see their syntax and lastly, we will understand their differences with some unique parameters.
Right Join
Right Join in SQL is used to return all the records from the rightmost table and the matching records from the leftmost table. In some scenarios, there may be a situation where there are no marches, then it will still include the rows from the right table but it will show the NULL values for the columns that are associated with the left table.
In the context of the query, below is the syntax of the Left Join.
Syntax:
SELECT columns
FROM left_table
RIGHT JOIN right_table ON
join_condition;
Now, let's see the example of Right Join:
Example of Right Join
Employee_Data Table
employee_id | employee_name | department_id |
---|
1 | Aditi | 101 |
2 | Bhakti | 102 |
3 | Vaishnavi | 101 |
Department_Data Table
department_id | department_name |
---|
101department_name | DBMS |
102 | Computer Network |
103 | Linux |
Query for Right Join
SELECT Employee_Data.employee_id, Employee_Data.employee_name, Department_Data.department_name
FROM Employee_Data
RIGHT JOIN Department_Data ON
Employee_Data.department_id = Department_Data.department_id;
Output:
employee_id | employee_name | department_name |
---|
1 | Aditi | DBMS |
3 | Vaishnavi | DBMS |
NULL | NULL | Linux |
2 | Bhakti | Computer Network |
Explanation
In the above example, Right Join includes all the rows from the right table (Department_Data) and matched them with the corresponding rows of the left table (Employee_Data). If there is no match, then the NULL value for the column is been displayed. From the above example, the 'Linux' department in the Department_Data table has no associated employee in the Employee_Table, so the NULL value is been displayed.
Right Outer Join
The Right Outer Join is mostly similar to the Right Join, and both these joins are interchangeably used. The keyword used here is "Outer", which is also optional and doesn't have any severe impact on the result set of the query.
In the context of the query, below is the syntax of the Right Outer Join.
Syntax
SELECT columns
FROM left_table
RIGHT OUTER JOIN right_table ON
join_condition;
Example of Right Outer Join
Let's consider the same tables used in the above Right Join Example:
Query for Right Outer Join
SELECT Employee_Data.employee_id, Employee_Data.employee_name, Department_Data.department_name
FROM Employee_Data
RIGHT OUTER JOIN Department_Data ON
Employee_Data.department_id = Department_Data.department_id;
Output:
employee_id | employee_name | department_name |
---|
1 | Aditi | DBMS |
NULL | NULL | Linux |
2 | Bhakti | Computer Network |
3 | Vaishnavi | DBMS |
Explanation
In the above example, all the records from the right table (Department_Data) are preserved, and the matching records or data from the right-most table (Employee_Data) are included. If there is no match, then the NULL values are displayed for the columns of the left table. The department 'DBMS' in the Department_Data table has the 2 employees (Aditi, Vaishnavi) associated with it, so their information is included in the result. The 'Linux' department has no employees, so the NULL values are displayed in the resultant table.
Right Join V/S Right Outer Join
Parameter | Right Join | Right Outer Join |
---|
Preserved Records | All the records from the right-most table in Database are preserved using Right Join. | All the records from the right-most table in Database are preserved using Right Outer Join. |
Non-Matching Records | Non-Matching Records from the left table are excluded if the query consists of Right Join. | Non-matching records from the left table are included, showing the NULL values for left table columns if the query consists of Right Outer Join. |
Join Keyword | The keyword used here is "RIGHT JOIN" | The keyword used here is "Right Outer Join" |
Result Focus | Right Join mainly focuses on the right table's data and its matching records. | Right Outer Join mainly focuses on combining the right table's data with the matching records from the left table. |
Syntax | SELECT columns FROM left_table RIGHT JOIN right_table ON join_condition; | SELECT columns FROM left_table RIGHT OUTER JOIN right_table ON join_condition; |
Conclusion
In conclusion, both right join and right outer join are quite the same thing in SQL. They both have different names for the same query operation. The right join matches the data from the right table to the left table and if the data does not match to the right table it fills the NULL value. Whereas the right outer join is the formal name of the right join operation, the right join is most commonly used. Understanding that these terms are the same, only the name differs is important to avoid further confusion and effectively use the join operation to combine data from tables.
Similar Reads
Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
DBMS Tutorial â Learn Database Management System Database Management System (DBMS) is a software used to manage data from a database. A database is a structured collection of data that is stored in an electronic device. The data can be text, video, image or any other format.A relational database stores data in the form of tables and a NoSQL databa
7 min read
Introduction of ER Model The Entity-Relationship Model (ER Model) is a conceptual model for designing a databases. This model represents the logical structure of a database, including entities, their attributes and relationships between them. Entity: An objects that is stored as data such as Student, Course or Company.Attri
10 min read
Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
Normal Forms in DBMS In the world of database management, Normal Forms are important for ensuring that data is structured logically, reducing redundancy, and maintaining data integrity. When working with databases, especially relational databases, it is critical to follow normalization techniques that help to eliminate
7 min read
ACID Properties in DBMS In the world of DBMS, transactions are fundamental operations that allow us to modify and retrieve data. However, to ensure the integrity of a database, it is important that these transactions are executed in a way that maintains consistency, correctness, and reliability. This is where the ACID prop
8 min read
Class Diagram | Unified Modeling Language (UML) A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact
12 min read
Introduction of DBMS (Database Management System) A Database Management System (DBMS) is a software solution designed to efficiently manage, organize, and retrieve data in a structured manner. It serves as a critical component in modern computing, enabling organizations to store, manipulate, and secure their data effectively. From small application
8 min read
Backpropagation in Neural Network Back Propagation is also known as "Backward Propagation of Errors" is a method used to train neural network . Its goal is to reduce the difference between the modelâs predicted output and the actual output by adjusting the weights and biases in the network.It works iteratively to adjust weights and
9 min read
3-Phase Inverter An inverter is a fundamental electrical device designed primarily for the conversion of direct current into alternating current . This versatile device , also known as a variable frequency drive , plays a vital role in a wide range of applications , including variable frequency drives and high power
13 min read