DEV Community

Martin Ndungu
Martin Ndungu

Posted on

SQL JOINs Explained Simply – From Beginner to Data Analyst

*SQL JOINs Explained Simply – From Beginner to Data Analyst
*

Introduction
When I first started learning SQL, JOINs felt confusing and overwhelming. But once I understood how they work, everything changed. JOINs are one of the most powerful tools in SQL they allow you to combine data from multiple tables and unlock real insights.

In this article, I’ll explain SQL JOINs in the simplest way possible using real examples from my project.

What is a JOIN?

A JOIN is used to combine rows from two or more tables based on a related column.

Think of it like this:

  • One table has students
  • Another table has exam results

A JOIN allows you to connect them and answer questions like:
👉 Which student scored what marks?

Types of SQL JOINs

  1. INNER JOIN (Most Common)

Returns only matching records from both tables.

Example:

SELECT 
    a.appointment_id,
    p.full_name,
    d.full_name,
    a.diagnosis
FROM city_hospital.appointments a
INNER JOIN city_hospital.patients p 
    ON a.patient_id = p.patient_id
INNER JOIN city_hospital.doctors d 
    ON a.doctor_id = d.doctor_id;
Enter fullscreen mode Exit fullscreen mode

✅ Only shows records where a match exists
❌ Drops unmatched data

  1. LEFT JOIN

Returns ALL records from the left table and matching ones from the right.

Example:

SELECT 
    p.full_name,
    a.appointment_date,
    a.diagnosis
FROM city_hospital.patients p
LEFT JOIN city_hospital.appointments a 
    ON p.patient_id = a.patient_id;
Enter fullscreen mode Exit fullscreen mode

✅ Shows ALL patients
✅ Even those without appointments


  1. RIGHT JOIN

Returns ALL records from the right table.

Example:

SELECT 
    d.full_name AS doctor_name,
    p.full_name AS patient_name
FROM city_hospital.appointments a
RIGHT JOIN city_hospital.doctors d 
    ON a.doctor_id = d.doctor_id
LEFT JOIN city_hospital.patients p 
    ON a.patient_id = p.patient_id;
Enter fullscreen mode Exit fullscreen mode

✅ Shows ALL doctors
✅ Even those who haven’t seen patients

  1. Finding Missing Data (Advanced Trick)

Want to find patients who NEVER had appointments?

SELECT 
    p.full_name,
    p.city
FROM city_hospital.patients p
LEFT JOIN city_hospital.appointments a 
    ON p.patient_id = a.patient_id
WHERE a.appointment_id IS NULL;
Enter fullscreen mode Exit fullscreen mode

🔥 This is a very important real-world technique.


Real-World Use Case

Imagine a hospital system:

  • Patients table → personal details
  • Doctors table → medical staff
  • Appointments table → interactions

Using JOINs, you can answer:

  • Which doctor treated which patient?
  • Which patients have never visited?
  • What diagnoses were given?

This is exactly how data analysts work in real companies.

Common Mistakes Beginners Make

❌ Forgetting the ON condition
❌ Joining wrong columns
❌ Using INNER JOIN when LEFT JOIN is needed
❌ Not understanding NULL results


Key Takeaways

  • INNER JOIN = matching records only
  • LEFT JOIN = keep everything from left
  • RIGHT JOIN = keep everything from right
  • JOINs are essential for real data analysis

Conclusion

JOINs are not just a SQL topic — they are a core skill for data analysis, business intelligence, and real-world problem solving.

Once you master JOINs, you move from writing queries to actually understanding data.

About This Project

This article is based on my SQL practice project using:

  • PostgreSQL
  • Real-world structured datasets
  • Topics including JOINs, Window Functions, and Data Analysis

Connect With Me

If you're also learning SQL or data analysis, feel free to connect and share ideas!

Top comments (0)