Outer Join

An OUTER JOIN retrieves all records from one or both tables, even if there are no matches, by filling missing values with NULLs.

Explanation:

There are three types of outer joins:

  1. LEFT OUTER JOIN (LEFT JOIN): Returns all records from the left table and matching records from the right. Non-matching records from the right table appear as NULL.
  2. RIGHT OUTER JOIN (RIGHT JOIN): Returns all records from the right table and matching records from the left. Non-matching records from the left table appear as NULL.
  3. FULL OUTER JOIN: Returns all records from both tables, filling missing values with NULLs.

Example:

SQL Query for LEFT OUTER JOIN:

sqlCopyEditSELECT Employees.name, Departments.department_name
FROM Employees
LEFT OUTER JOIN Departments ON Employees.dept_id = Departments.dept_id;

Output:

namedepartment_name
AliceHR
BobIT
CharlieNULL

Here, Charlie appears even though thereโ€™s no matching dept_id in Departments.