How do you use LEFT JOIN with an example?

In MySQL, a LEFT JOIN allows you to return all records from the left table and the matched records from the right table. If there is no match, NULL values will be returned for columns from the right table.

Here’s a simple example:

SELECT employees.name, departments.department_name FROM employees LEFT JOIN departments ON employees.department_id = departments.id;

In this example, we are selecting employee names along with their department names. If an employee does not belong to a department, the department name will return as NULL.


MySQL LEFT JOIN SQL database tutorials