![]() |
Database management system or DBMS is the application software that allows us with its feature to store and manage the data which is collected from different types of sources. Now these sources can be from social media, user feedback, real-time generated data, etc. Database Management System follows the standardized model, which is a relational model and it uses the SQL language which is a Structured Query Language through which the programmers or developers can write the custom query and retrieve or store the data in the database in the form of tables. There can be some scenarios where we need to return the result by combining the two tables. So for that purpose, joins come into the picture. Joins in the database are used to combine two tables. So in this article, we will go through the detailed information about two types of important joins in DBMS, that is Left Join and Right Join. Left JoinLeft Join in SQL language is used to retrieve all the data or records from the left table and the matching data or records from the right table. In some cases, if there is no match of data or the record as per the given query by the programmer, then the left join will still display or return the rows from the left table and will show the NULL values for the columns of the right table. In the context of the query, below is the syntax of the Left Join. Syntax
Example Employee_Data Table
Department_Data Table
Query for Left JoinSELECT Employee_Data.emp_name, Employee_Data.emp_dept, Department_Data.location_name Result
ExplanationIn the above results, we can see that data or the records from the (Employee_Data) table are included, and all the matching records from the (Department_Data) table are also included. One f the record has no match for the department_name as Finance, so its corresponding location_name is filled with the NULL value. Right JoinRight Join in SQL is used to retrieve all the data or records that are stored from the rightmost table and the matching records from the leftmost table. In some cases, there might be a situation where there are no matches of the data, then in this case Roght Join will still include the rows from the right table but it will display 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 Right Join. Syntax
Now, let’s see the example of Right Join: Example: Let’s consider the same tables used in the above Left Join Example: Query for Right JoinSELECT Employee_Data.emp_name, Employee_Data.emp_dept, Department_Data.location_name Result
ExplanationIn the above results, we can see that all the records or the data from the (Department_Data) table are been displayed and all the matching records from the (Employee_Data) table are also displayed. There is no match for the department Marketing in the Employee_Data table, so the corresponding emp_name is filled with the NULL value and the emp_dept has its original value which is Marketing/Sales. Left Join v/s Right Join
FAQs: Right Join vs Left Join1. What happens to the non-matching rows when we use Right Join?
2. Can we use Left Join and Right Join in the same query?
3. Whether it is allowed to use LeftJoin and Right Join with more than 2 tables in a database?
4. How do the duplicate records been handled by Left Join and Right Join?
|
Reffered: https://www.geeksforgeeks.org
DBMS |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 12 |