JOIN

SQL JOIN Keyword

The JOIN clause combines rows from two or more tables based on a related column between them.

Tutorials dojo strip

SQL INNER JOIN Example

This query returns records from both Patients and Appointments tables where there is a matching patient_id.

SELECT Patients.first_name AS patient_name, Appointments.appointment_id
FROM Patients
INNER JOIN Appointments ON Patients.patient_id = Appointments.patient_id;

SQL LEFT JOIN Example

This query returns all records from Doctors, along with matching Appointments, if any.

SELECT Doctors.doctor_id, Appointments.appointment_id
FROM Doctors
LEFT JOIN Appointments ON Doctors.doctor_id = Appointments.doctor_id;

SQL RIGHT JOIN Example

This query retrieves all doctor_id values from the Doctors table and any matching appointment_id from the Appointments table, including doctors who do not have any appointments.

SELECT Appointments.appointment_id, Doctors.doctor_id
FROM Appointments
RIGHT JOIN Doctors ON Appointments.doctor_id = Doctors.doctor_id;

Note: This code does not work in SQLite or the TechKubo playground.


SQL FULL OUTER JOIN Example

This query retrieves all first_name values from the Patients table and all appointment_id values from the Appointments table, including patients without appointments and appointments without matching patients.

SELECT Patients.first_name, Appointments.appointment_id
FROM Patients
FULL OUTER JOIN Appointments ON Patients.patient_id = Appointments.patient_id;

Note: This code does not work in SQLite or the TechKubo playground.

SQL JOIN Labs

Tutorials dojo strip
Scroll to Top