1.

State the difference between inner join and left join.

Answer»

  • Inner Join: This join generates datasets that contain matching records in both tables (left and right). By using an inner join, only the rows that match between each of the tables are returned; all non-matching rows are removed.

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 perform INNER JOIN on these two tables using a SELECT statement, as shown below: 

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

Output: 

Emp_NameEmp_NoEmp_ProfileEmp_CountryEmp_Join_Date
Ashish Kaktan9450425345Content WriterGermany2021-04-20
Shantanu Khandelwal9020330023Data AnalystIndia2022-12-11
Khanak Desai8451004522Software EngineerIndia2022-01-03

  • 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 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_Country
FROM Tb1_Employee LEFT JOIN Tb2_Employment
ON Tb1_Employee.Emp_ID=Tb2_Employment.Emp_ID;

Output:

Emp_NameEmp_NoEmp_ProfileEmp_Country
Ashish Kaktan9450425345Content WriterGermany
Raj Choudhary8462309621NullNull
Vivek Oberoi7512309034NullNull
Shantanu Khandelwal9020330023Data AnalystIndia
Khanak Desai8451004522Software EngineerIndia



Discussion

No Comment Found