top of page

Welcome
to NumpyNinja Blogs

NumpyNinja: Blogs. Demystifying Tech,

One Blog at a Time.
Millions of views. 

A Beginner Guide to SQL Joins Using Medical Data

Jan 14
4 min read

When you work with hospital systems, you often deal with many connected tables: patients, doctors, appointments, lab tests, medical bills, medicines, insurance details, and more. To combine data from these tables, we use SQL JOINS.


In healthcare systems, information is never stored in one single big table. Hospitals separate data into different tables to keep it clean, organized, secure, and easy to maintain.

For example:

  • Patients are stored in one table

  • Doctors are stored in another table

  • Appointments are stored separately

  • Lab test records are in another table

  • Billing and payments are kept in their own table

  • Medicines and prescriptions are stored elsewhere


Because the information is spread out like this, you cannot get a complete report directly from a single table. To create useful outputs — like patient summaries, doctor schedules, lab test results, billing reports, insurance claims, pharmacy usage, or hospital dashboards — you must join these tables together.


JOINS allow the system to connect related data such as which patient visited which doctor, which tests were done, which medicines were prescribed, what bills were generated, and which insurance company covers the patient. Without JOINS, you would only see scattered pieces of information. With JOINS, you get the full picture in one combined result.


What is a JOIN?

A JOIN is used to merge data from two or more tables based on a related column — usually an ID.

Example:patients.patient_id = appointments.patient_id


Common Healthcare Tables

To understand JOINS, imagine these tables:

1. Patients

patient_id

name

age

gender

2. Doctors

doctor_id

doctor_name

department

3. Appointments

appointment_id

patient_id

doctor_id

appointment_date

4. Lab_tests

test_id

patient_id

test_name

result

TYPES OF JOINS IN HEALTHCARE (WITH EXAMPLES)

INNER JOIN

An INNER JOIN returns only the data that appears in both tables, focusing only on matching records.



✔ Example: Get the list of patients and their doctors

SELECT p.name AS patient_name,
       d.doctor_name,
       a.appointment_date
FROM patients p
INNER JOIN appointments a 
       ON p.patient_id = a.patient_id
INNER JOIN doctors d 
       ON a.doctor_id = d.doctor_id;

Meaning:

Show only patients WHO HAVE an appointment.


2. LEFT JOIN

A LEFT JOIN returns all the data from the left table and includes matching data from the right table, placing NULL where no match exists.



✔ Example: List all patients and their appointments (if any)

SELECT p.name,
       a.appointment_date
FROM patients p
LEFT JOIN appointments a 
       ON p.patient_id = a.patient_id;

Meaning:

Shows everyone. If a patient did not come for an appointment, appointment_date will be NULL.


Why LEFT JOIN is most useful in Health Care:

To show all patients even if

✔ No doctor assigned

✔ No appointment

✔ No lab test

This is useful for hospital dashboards and patient summary pages.


3. RIGHT JOIN

Opposite of LEFT JOIN.A RIGHT JOIN works similarly but prioritizes the right table instead.



✔ Example: Show all appointments, including those with missing patient data

SELECT p.name, 
       a.appointment_date
FROM patients p
RIGHT JOIN appointments a
       ON p.patient_id = a.patient_id;

Meaning:

Shows all appointments even If a patient _id is NULL.


4. FULL OUTER JOIN

A FULL OUTER JOIN returns all records from both tables, including unmatched data on both sides, filling missing parts with NULL.



✔ Example: All patients + all appointments (whether linked or not)

SELECT p.name,
       a.appointment_date
FROM patients p
FULL OUTER JOIN appointments a
       ON p.patient_id = a.patient_id;

Meaning:

Useful when cleaning data or finding missing records.


5.CROSS JOIN

A CROSS JOIN combines every row of one table with every row of another table.This creates all possible combinations.



✔ Example (Patients × Doctors)

To generate every patient × every doctor combination — useful for scheduling, assigning doctors, or testing data.

SELECT 
    p.name AS patient_name,
    d.doctor_name
FROM patients p
CROSS JOIN doctors d;

If you have 10 patients and 5 doctors, the result will show 50 combinations.


6.NATURAL JOIN

A NATURAL JOIN automatically joins tables based on columns that have the same name.

It does not require ON condition.

Note: Natural joins should be used only when column names match perfectly.



✔ Example with Patients & Appointments

NATURAL JOIN will join using patient_id (because both tables have this column name).

SELECT 
    name,
    appointment_date
FROM patients
NATURAL JOIN appointments;

Result:

  • Patient name

  • Appointment date Joined automatically on patient_id


Comparison Table of SQL JOINS:

JOIN Type

What It Does

When to Use

INNER JOIN

Returns only the rows that match in both tables

When you want only matching data from both tables

LEFT JOIN

Returns all rows from the left table, and matching rows from the right table; non-matching rows show NULL

When left table’s data is important even if right table has no match

RIGHT JOIN

Returns all rows from the right table, and matching rows from the left table; non-matching rows show NULL

When right table’s data is important even if left table has no match

FULL OUTER JOIN

Returns all rows from both tables; non-matching rows show NULL

When you need all data from both tables

CROSS JOIN

Returns all possible combinations of rows from the two tables

When you want every combination of data

NATURAL JOIN

Automatically joins tables based on columns with the same name

When tables have columns with identical names and you want automatic matching


JOINS with MULTIPLE TABLES

This is very common in hospital reports.

✔ Example: Patient → Appointment → Doctor → Lab Test (full patient summary)

SELECT 
    p.name AS patient_name,
    d.doctor_name,
    a.appointment_date,
    lt.test_name,
    lt.result
FROM patients p
LEFT JOIN appointments a
       ON p.patient_id = a.patient_id
LEFT JOIN doctors d
       ON a.doctor_id = d.doctor_id
LEFT JOIN lab_tests lt
       ON p.patient_id = lt.patient_id;


Real-world Healthcare Use Cases of JOINS

  • Patient summary dashboards

  • Doctor’s daily appointment list

  • Lab test tracking

  • Medical billing + insurance coverage reports

  • Pharmacy medicine dispensing details

  • Emergency room intake reports

  • Hospital admin reporting

  • Data cleanup (missing patient/doctor data)

 
 

+1 (302) 200-8320

NumPy_Ninja_Logo (1).png

Numpy Ninja Inc. 8 The Grn Ste A Dover, DE 19901

© Copyright 2025 by Numpy Ninja Inc.

  • Twitter
  • LinkedIn
bottom of page