The SQL Left Join or Left Outer Join allows you to retrieve all rows from the left table and the matched rows from the right table.
SQL Left Join Syntax
SELECT column_list FROM left_table LEFT JOIN right_table ON left_table.common_column = right_table.common_column;
SQL Left Join Retrieving All Patients and Their Appointments Example
This query retrieves all patients from the Patients table, even those who do not have a corresponding entry in Appointments table.
SELECT p.first_name AS "Patient Name", a.appointment_date AS "Appointment Date" FROM Patients AS p LEFT JOIN Appointments AS a ON p.patient_id = a.patient_id;
SQL Left Join Listing All Doctors and Their Patients’ Names
This query shows all doctors, even those who currently do not have any patients assigned to them. For doctors without patients, the Patient Name filed will be NULL.
SELECT d.first_name AS "Doctor Name", p.first_name AS "Patient Name" FROM Doctors AS d LEFT JOIN Appointments AS a ON d.doctor_id = a.doctor_id LEFT JOIN Patients AS p ON a.patient_id = p.patient_id;