SQL FOUNDATIONS:Outer Joins (LEFT, RIGHT, FULL)
Keep unmatched rows with LEFT, RIGHT, and FULL joins.
SQL Outer Joins (LEFT, RIGHT, FULL) Explained with Examples + Practice Questions
When to Use Outer Joins
- Use LEFT JOIN to keep all rows from the left table
- Use RIGHT JOIN to keep all rows from the right table
- Use FULL JOIN to keep all rows from both tables
Outer joins are used when you do not want to lose unmatched data.
Example Dataset
Table: employees
Table: departments
LEFT JOIN
Returns:
- All rows from left table (employees)
- Matching rows from right table
- If no match → NULL
Example
sqlSELECT e.name, d.department_nameFROM employees eLEFT JOIN departments dON e.department_id = d.id;
RIGHT JOIN
Returns:
- All rows from right table (departments)
- Matching rows from left table
- If no match → NULL
Example
sqlSELECT e.name, d.department_nameFROM employees eRIGHT JOIN departments dON e.department_id = d.id;
FULL JOIN
Returns:
- All rows from both tables
- Matches where possible
- NULL where no match
Example
sqlSELECT e.name, d.department_nameFROM employees eFULL JOIN departments dON e.department_id = d.id;
LEFT vs RIGHT vs FULL
Common Use Cases
- LEFT JOIN → find missing data
- RIGHT JOIN → reverse analysis
- FULL JOIN → full comparison
Finding Unmatched Rows (Important)
Employees without department
sqlSELECT e.nameFROM employees eLEFT JOIN departments dON e.department_id = d.idWHERE d.id IS NULL;
Departments without employees
sqlSELECT d.department_nameFROM departments dLEFT JOIN employees eON e.department_id = d.idWHERE e.id IS NULL;
Key Concepts
- INNER JOIN removes unmatched rows
- OUTER JOIN keeps unmatched rows
- NULL indicates no match
- LEFT JOIN is most commonly used
Common Questions (FAQ)
What is LEFT JOIN
Returns all rows from left table and matching rows from right table.
What is RIGHT JOIN
Returns all rows from right table and matching rows from left table.
What is FULL JOIN
Returns all rows from both tables, matched or not.
When to use LEFT JOIN
When you want to find missing or unmatched data.
Internal Learning Links
- Learn INNER JOIN
- Learn WHERE filtering
- Learn GROUP BY
Practice CTA
Practice SQL JOIN problems on Schoolabe to master real-world data relationships.
SQLite Note
SQLite does not support RIGHT JOIN or FULL JOIN directly. In the exercises below, we use equivalent queries (RIGHT JOIN = LEFT JOIN with tables swapped; FULL JOIN = UNION of LEFT + RIGHT).
Outer Joins (LEFT, RIGHT, FULL) Missions
Solve exercises in sequence to unlock the next mission.
LEFT JOIN employees + departments
Perform LEFT JOIN to show all employees with department names.
Solve MissionRIGHT JOIN departments + employees
Perform RIGHT JOIN to show all departments with employee names.
Employees without department
Find employees who do not belong to any department.
Departments without employees
Find departments that have no employees.
FULL JOIN employees + departments
Show all employees and departments using FULL JOIN.
Employees with departments only
Retrieve employee name and department name where department exists.
All employees by salary
Retrieve all employees and sort by salary.
Count employees per department
Count employees per department using LEFT JOIN.
Departments with >1 employee
Find departments with more than 1 employee.
Unassigned employees (Interview)
Find employees who are not assigned to any department.