SQL FULL JOIN
FULL JOIN (or FULL OUTER JOIN) returns all rows from both tables. If thereβs no match, unmatched columns return NULL. β Combines the effects of LEFT JOIN and RIGHT JOIN. πΉ Basic Syntax βοΈ Some DBMS (like MySQL) donβt support FULL JOIN directly β use a workaround with UNION. πΉ Example: List All Employees and […]
FULL JOIN (or FULL OUTER JOIN) returns all rows from both tables. If thereβs no match, unmatched columns return NULL.
β
Combines the effects of LEFT JOIN and RIGHT JOIN.
πΉ Basic Syntax
SELECT columns
FROM table1
FULL JOIN table2
ON table1.common_column = table2.common_column;βοΈ Some DBMS (like MySQL) donβt support FULL JOIN directly β use a workaround with UNION.
πΉ Example: List All Employees and All Departments (Matched or Not)
SELECT
e.employee_name,
d.department_name
FROM employees e
FULL JOIN departments d
ON e.department_id = d.department_id;- If an employee has no department β
department_nameisNULL - If a department has no employee β
employee_nameisNULL
πΉ MySQL Workaround for FULL JOIN
SELECT e.employee_name, d.department_name
FROM employees e
LEFT JOIN departments d ON e.department_id = d.department_id
UNION
SELECT e.employee_name, d.department_name
FROM employees e
RIGHT JOIN departments d ON e.department_id = d.department_id;π§ Quick Recap
| Key Point | Explanation |
|---|---|
FULL JOIN | Returns all rows from both tables |
| No match? | Missing side columns are filled with NULL |
| Use case | Useful when you want to show all records, matched or not |
| MySQL note | Use LEFT JOIN + RIGHT JOIN with UNION workaround |
π§© Use FULL JOIN to see the complete picture from both tables
Was this helpful?
Thanks for your feedback!