The LEFT JOIN keyword in MySQL returns all records from the left table and the matching records from the right table. If there's no match, the right table's columns will contain NULL.
NULL.
// Syntax for LEFT JOIN: keep all rows from table1 and match table2 where possible
SELECT columns
FROM table1
LEFT JOIN table2
ON table1.common_column = table2.common_column;
// Get all employees and their department names (include employees with no department)
SELECT employees.name, departments.department_name
FROM employees
LEFT JOIN departments
ON employees.department_id = departments.department_id;
// Get all products and their related order quantities (include products never ordered)
SELECT products.product_name, orders.quantity
FROM products
LEFT JOIN orders
ON products.product_id = orders.product_id;
employees row has no matching departments row, department_name will be NULL.WHERE cautiously — applying a WHERE clause to a right-table column can effectively turn the LEFT JOIN into an INNER JOIN unless you account for NULLs (e.g. WHERE departments.department_name IS NULL to find unmatched rows).IS NULL on a right-table column.