SQL Left Join

HTML
CSS
C#
SQL

SQL Left Join

The join operation in SQL is used to combine multiple tables together into a single table. If we use left join to combine two different tables, then we will get all the records from the left table. But we will get only those records from the right table, which has the corresponding key in the left table. Rest other records in the right table for which the common column value doesn’t match with the common column value of the left table; then, it is displayed as NULL.

Example:

SELECT TableName1.columnName1, TableName2.columnName2 FROM TableName1 LEFT JOIN TableName2 ON TableName1.ColumnName = TableName2.ColumnName;

Consider we have the following tables with the given data:

Write a query to perform the left join operation considering the employee table as the left table and the department table as the right table.

SELECT e.EmployeeID, e.Employee_Name, e.Employee_Salary, d.DepartmentID, d.Department_Name FROM employee e LEFT JOIN department d ON e.EmployeeID = d.Employee_ID;

We have used the SELECT command to retrieve EmployeeID, Employee_Name, Employee_Salary, DepartmentID, Department_Name present in the employee and department table. Then we have used the LEFT JOIN keyword to perform the left join operation on the employee and department table where ‘e’ and ‘d’ are aliases. These two tables are joined on the column EmployeeID which is present in both the tables.

 You will get the following output:

EmployeeID, Employee_Name, Employee_Salary, DepartmentID, Department_Name are retrieved from employee and department tables. All the records from the employee table are retrieved. Only those records that have a corresponding EmployeeID in the employee table are retrieved from the department table. Rest other records in the department table for which an EmployeeID doesn’t match with the EmployeeID of the employee table; then, it is displayed as NULL.

Write a query to perform the left join operation considering the loan table as the left table and the borrower table as the right table.

SELECT l.LoanID, l.Branch, l.Amount, b.CustID, b.CustName FROM Loan l LEFT JOIN Borrower b ON l.LoanID = b.LoanID;

We have used the SELECT command to retrieve LoanID, Branch, Amount, CustID, CustName present in the loan and borrower table. Then we have used the LEFT JOIN keyword to perform the left join operation on the loan and borrower table where ‘l’ and ‘b’ are aliases. These two tables are joined on the column LoanID which is present in both the tables.

You will get the following output:

LoanID, Branch, Amount, CustID, CustName are retrieved from loan and borrower tables. All the records from the loan table are retrieved. Only those records that have a corresponding LoanID in the loan table are retrieved from the borrower table. Rest other records in the borrower table for which a LoanID doesn’t match with the LoanID of the loan table; then, it is displayed as NULL.

Write a query to perform the left join operation considering the customer table as the left table and the orders table as the right table.

SELECT c.Customer_ID, c.Name, c.Age, c.Salary, o.Order_ID, o.Order_Date, o.Amount FROM customer c LEFT JOIN orders o ON c.Customer_ID = o.Customer_ID;

We have used the SELECT command to retrieve Customer_ID, Name, Age, Salary, Order_ID, Order_Date, Amount present in customer and orders table. Then we have used the LEFT JOIN keyword to perform left join operation on the customer and orders table where ‘c’ and ‘o’ are aliases. These two tables are joined on the column Customer_ID which is present in both the tables.

 You will get the following output:

Customer_ID, Name, Age, Salary, Order_ID, Order_Date, Amount are retrieved from customer and orders tables. All the records from the customer table are retrieved. Only those records that have a corresponding Customer_ID in the customer table are retrieved from the orders table.

Course Video

NOTE: Practice below questions on site editor.

1. Write a SQL query to get all storeID, store Name with SalesPersonID, Bonus from Store and Sales.SalesPerson tables.

NOTE: OUTPUT on SQL Editor Count: 100 Rows.
OUTPUT 

2. Write a SQL query to get all salesPersonId, salesLastYear, salesYTD with quotaDate, salesquota, TerritoryName, CountryRegionCode from sales.SalesPerson, sales.SalesPersonQuotaHistory and sales.SalesTerritory.

NOTE: OUTPUT on SQL Editor Count: 108 Rows.
OUTPUT 

3. Write a query to join SalesStore, SalesPerson, SalesPersonQuotaHistory and SalesTerritory table to retrieve two columns from each table.

NOTE: OUTPUT on SQL Editor Count: 869 Rows.

OUTPUT

4. Write SQL query to get all specialOfferProduct details with description, type from sales.SpecialOfferProduct and sales.

NOTE: OUTPUT on SQL Editor Count: 100 Rows.

OUTPUT

5. Write a query to join salesOrderHeader, SalesOrderDetail, SpecialOfferproduct and SpecialOffer table to retrieve two columns from each table.

NOTE: OUTPUT on SQL Server Count: 5154 Rows,

OUTPUT on SQL Editor Count: 190 Rows.

OUTPUT