The SQL LEFT JOIN keyword returns all rows from the left table, even if there are no matches in the right table.
In this example, employeeID is a primary key in the table called "Employees". You'll notice that employeeID also exists in the "Departments" table, but as a foreign key.
Using an SQL Join, we can combine these two tables into one when presenting the results of a query.
In this example, using a LEFT JOIN, we can build a query to display the all of
the departments and employees even though not every department has an employee
assigned to it, such as in the case of 'Operations'. The two tables are joined by the 'employeeID' field.
We can use the LEFT JOIN to bind this data together.
SELECT tableName#.columnName#, tableName#.columnName#, etc...|
LEFT JOIN tableName2
ON tableName1.columnName# = tableName2.columnName#
List the departments and their assigned employees.
SELECT Departments.deptName, Employees.employeeName |
LEFT JOIN Employees
ON Departments.employeeID = Employees.employeeID
You should immediately notice that 'Operations' from the Departments table.
This is because there is there are no employees assigned to 'Operations' and we had
specific a LEFT JOIN in the query.
If there are rows in "Departments" that do not have matches in "Employees", the
records from Departments will be returned in the result set.
Recommended Books & Training Resources