A Right Join or Right Outer Join returns all records from the right table and the matched records from the left table. If there’s no match, the result is NULL on the left side.
SQL Right Join Syntax
SELECT column1, column2, ... FROM table1 RIGHT JOIN table2 ON table1.column_name = table2.column_name;
SQL Right Join Matching Patients and Appointments Example
This query shows all patients, along with their appointments. If a patient has no apppointments scheduled, their name will still appear, but the appointments fields will be NULL.
SELECT a.appointment_id, p.first_name, p.last_name FROM Appointments a RIGHT JOIN Patients p ON a.patient_id = p.patient_id;
SQL Right Join Patients with Medical Records
This query highlights all patients and their medical records. If a patient has no medical record, their details will appear, but the records infortmation will be NULL.
SELECT m.record_id, p.first_name, p.last_name FROM MedicalRecords m RIGHT JOIN Patients p ON m.patient_id = p.patient_id;