The left Join of SQL
The left join is the type of join that returns all rows from the left table (the table in the select from) whereas only matched rows from the other table are retrieved.
As it returns all rows from the first table / left table, the column values for the right table will be shown as Null that do not match.
Note: The right table is the one used in the Join clause.
An example of let join
Syntax of SQL left join clause
The general syntax of the left join SQL clause is:
Select * from table_name1
left Join table_name2 ON table_name1.common_field = table_name2.common_field
Example of using left join
The following example shows how to use the left join in SQL. For our example, we will use the departmental store scenario (Explained in SQL joins chapter). Consider a departmental store’s database tables:
- tbl_employee
- tbl_customers_orders
- tbl_products
Where tbl_employee stores the employee’s data, the tbl_prodcuts stores the products in store and tbl_customers_orders stores the orders placed by customers. The tbl_customers_orders stores the information like customer_id, products_id and also employee_id of the employee associated with that order.
In the following example, we will retrieve all records from the left table (tbl_employee table after the select from statement, which is towards left) and only matched records from joined or right table, tbl_customers_orders by using the SQL left join type.
The left join query:
1
|
select * from tbl_employee TE left join tbl_customers_orders TC ON TE.emp_id=TC.emp_id
|
See the graphic of all tables and left join
As you can see in the above graphic, all rows are retrieved from the tbl_emploee whereas columns for tbl_customers_orders are filled with the Null, that are not matched..
Also see – SQL Joins | SQL right join | SQL inner join
Leave A Comment?