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:
- 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.
- 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.
- 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:
name | department_name |
---|---|
Alice | HR |
Bob | IT |
Charlie | NULL |
Here, Charlie
appears even though thereโs no matching dept_id
in Departments
.
Leave a Reply