A FULL OUTER JOIN combines the behaviour or of both LEFT JOIN and RIGHT JOIN. It returns:
- All matching records from both tables.
- Records from the left table that have no match.
- Records from the right table that have no match.
- Whenever a match doesn't exist, SQL fills the missing values with
NULL.
For this exercise, I joined the visits and doctors tables to display hospital visit IDs together with the responsible doctor's name. The goal was to understand how SQL preserves records from both tables while still connecting related data whenever a match is available. Because I had already normalized my database and established proper relationships, every visit had a matching doctor. As a result, the output looked very similar to an INNER JOIN.
-- I want to know all the visit ID and the doctor names
select visit_id, doctor_name from visits v
full outer join doctors d
on v.doctor_id = d.doctor_id
order by visit_id
limit 40;
GitHub link: https://github.com/KratosOCS/Hospital-Records-Analysis

Top comments (0)