DEV Community

Cover image for RIGHT JOIN in SQL
Dennis Wachira
Dennis Wachira

Posted on

RIGHT JOIN in SQL

A RIGHT JOIN returns all records from the right table and the matching records from the left table. If there is no matching record in the left table, SQL returns NULL for those columns instead of removing the row from the results.
For this exercise, I used my Hospital Records database to connect the visits and doctors tables. My goal was to display every doctor together with their corresponding hospital visits.
Although my current dataset contains matching records for every doctor and visit, this exercise helped me understand that RIGHT JOIN guarantees that every record from the right table is included in the result.

select doctor_name, visit_id, v.date_of_visit
from visits v 
right join doctors d 
on d.doctor_id = v.doctor_id
order by v.visit_id;

Enter fullscreen mode Exit fullscreen mode

GitHub link: https://github.com/KratosOCS/Hospital-Records-Analysis

Top comments (0)