(because sometimes you need everything from both sides)
๐ง Quick Syntax
SELECT columns
FROM table1
FULL JOIN table2
ON table1.column = table2.column;
This says:
“Give me all rows from both tables, and match anything where possible. If thereโs no match, show NULL
.”
๐ Imagine You Have These Two Tables
๐ง employees
table:
emp_id | name |
---|---|
1 | Olivia |
2 | Mason |
3 | Zoe |
๐ ๏ธ projects
table:
project_id | emp_id | project_name |
---|---|---|
201 | 1 | Website |
202 | 2 | App |
203 | 4 | Database |
Now, you want to know which employees are assigned to projects, but you also want to see any employees who aren’t assigned, and any projects without employees.
๐ง FULL JOIN Syntax
SELECT employees.name, projects.project_name
FROM employees
FULL JOIN projects
ON employees.emp_id = projects.emp_id;
SQL will give you everything from both tables.
If thereโs no match, you get NULL
.
โ Result
name | project_name |
---|---|
Olivia | Website |
Mason | App |
Zoe | NULL |
NULL | Database |
Notice:
– Zoe
is an employee, but doesnโt have a project, so sheโs shown with NULL
for project_name
.
– The Database
project doesnโt have a matching employee, so itโs shown with NULL
for name
.
๐ LEFT JOIN vs RIGHT JOIN vs FULL JOIN
Type | Whoโs Shown |
---|---|
LEFT JOIN | All rows from the left, matches from the right |
RIGHT JOIN | All rows from the right, matches from the left |
FULL JOIN | All rows from both tables, matches or not |
๐ง Recap
FULL JOIN
gives you all rows from both tables, withNULL
for missing matches- Use
FULL JOIN
when you need everything from both tables โ whether or not thereโs a match