SQL -Structured Query Language - SQL Joins - Left Join Tutorial
Left Join or Left Outer Join is used to retrieve all the records from the left table ( table which is mentioned first – table1) and the records that are matched from the right table( table which is mentioned second – table2).
We can perform left joins in multiple tables.
The syntax for left joins is-
SELECT column_name1,column_name2,..,column_nameN
FROM
table_1 LEFT JOIN table_2
ON
table_1.column_name = table_2.column_name;
For example-
SELECT *
FROM
employees
LEFT JOIN
job_history
WHERE
employees.employee_id = job_history.employee_id;
Better to use AS (Alias) for the long table name-
SELECT e.first_name,e.email,e.salary,jh.start_date,jh.end_date
FROM
employees as e
left join
job_history as jh
WHERE
e.employee_id = jh.employee_id;
Here we will get all records from the left table (employees) and only those records where id will get matched in both employees and job_history table.
Another record that is not matched in the right table (table2) will not get retrieved.