1.

State difference between left join and right join.

Answer»

  • Left Join: It returns datasets that have matching records in both tables (left and right) plus non-matching rows from the left table. By using a left join, all the records in the left table plus the matching records in the right table are returned.

Example: Let's take a look at two tables. Here’s the Tb1_Employee table.

Emp_IDEmp_NameEmp_No
101Ashish Kaktan9450425345
102Raj Choudhary8462309621
103Vivek Oberoi7512309034
104Shantanu Khandelwal9020330023
105Khanak Desai8451004522

Here's the Tb2_Employment table. 

Emp_IDEmp_ProfileEmp_CountryEmp_Join_Date
101Content WriterGermany2021-04-20
104Data AnalystIndia2022-12-11
105Software EngineerIndia2022-01-03
108Development ExecutiveEurope2023-02-15
109Marketing ManagerMexico2020-05-23

Let’s now perform LEFT JOIN on these two tables using a SELECT statement, as shown below: 

SELECT Tb1_Employee.Emp_Name, Tb1_Employee.Emp_No, Tb2_Employment.Emp_Profile, Tb2_Employment.Emp_Join_Date
FROM Tb1_Employee LEFT JOIN Tb2_Employment
ON Tb1_Employee.Emp_ID=Tb2_Employment.Emp_ID;

Output:

Emp_NameEmp_NoEmp_ProfileEmp_Join_Date
Ashish Kaktan9450425345Content Writer2021-04-20
Raj Choudhary8462309621NullNull
Vivek Oberoi7512309034NullNull
Shantanu Khandelwal9020330023Data Analyst2023-02-15
Khanak Desai8451004522Software Engineer2020-05-23

  • Right Join: It returns datasets that have matching records in both tables (left and right) plus non-matching rows from the right table. By using a left join, all the records in the right table plus the matching records in the left table are returned.

Example: Let’s now perform RIGHT JOIN on these two tables using a SELECT statement, as shown below: 

SELECT Tb1_Employee.Emp_Name, Tb1_Employee.Emp_No, Tb2_Employment.Emp_Profile, Tb2_Employment.Emp_Join_Date
FROM Tb1_Employee RIGHT JOIN Tb2_Employment
ON Tb1_Employee.Emp_ID=Tb2_Employment.Emp_ID;

Output:

Emp_NameEmp_NoEmp_ProfileEmp_Join_Date 
Ashish Kaktan9450425345Content Writer2021-04-20
Shantanu Khandelwal9020330023Data Analyst2022-12-11
Khanak Desai8451004522Software Engineer2022-01-03
NullNullDevelopment Executive2023-02-15
NullNullMarketing Manager2020-05-23



Discussion

No Comment Found