A LEFT JOIN retrieves all rows from the left table (employees) and the matching rows from the right table (departments). If no match is found, the result still includes the left table’s row, but the right table’s columns will show NULL.
Show all employees, even those without a department:
SELECT e.emp_name, d.dept_name
FROM employees e
LEFT JOIN departments d
ON e.dept_id = d.dept_id;
Output: Includes Sneha with NULL for department.
Does Order Matter in LEFT JOIN?
LEFT JOIN always keeps all rows from the left table and only the matching rows from the right table. If there is no match, the right table’s columns are filled with NULL.
In INNER JOIN, the order of tables does not matter, because only rows with matches from both sides are returned. But in LEFT JOIN, order does matter because the join guarantees that all rows from the left table will appear in the result.
Query 1 (Employees as left table)
SELECT e.emp_name, d.dept_name
FROM employees e
LEFT JOIN departments d
ON e.dept_id = d.dept_id;
All employees are returned. If an employee has no department, dept_name will be NULL.
Query 2 (Departments as left table)
SELECT e.emp_name, d.dept_name
FROM departments d
LEFT JOIN employees e
ON e.dept_id = d.dept_id;
All departments are returned. If a department has no employees, emp_name will be NULL