Chapter 9: Joins

Joins are what make relational databases magical. They let us connect different tables using common columns (usually primary key and foreign key) and combine data from multiple tables in one beautiful query.

Today we’ll learn:

  • INNER JOIN – Only matching rows
  • LEFT JOIN / RIGHT JOIN – Keep all from one side
  • FULL OUTER JOIN – (MySQL doesn’t have it directly, but we emulate it)
  • SELF JOIN – Join a table with itself
  • CROSS JOIN – Cartesian product (every row with every row)
  • Multiple table joins – Joining 3+ tables

First – let’s create two related tables with sample data

We’ll use our coaching_class database and create two tables:

  1. students (we already have it)
  2. courses (new table)
  3. enrollments (junction table that connects students and courses)

Run these commands to set up the data:

SQL

Now we have:

  • 10 students
  • 5 courses
  • 8 enrollments (some students enrolled in multiple courses, some courses have multiple students)

1. INNER JOIN – Only Matching Rows from Both Tables

INNER JOIN returns only the rows where there is a match in both tables.

Syntax:

SQL

Example – Show students with their enrolled courses

SQL

Result: Only the 8 students who have enrolled in at least one course will appear (Rohan Mehta, Meera Desai, Arjun Reddy are not shown because they have no enrollments).

2. LEFT JOIN – Keep ALL Rows from Left Table

LEFT JOIN (or LEFT OUTER JOIN) keeps all rows from the left table, and matches from the right table. If no match → NULL in right columns.

Example – Show ALL students and their courses (even those with no courses)

SQL

Result: All 10 students appear. Students with no enrollments will show NULL in course_name and enrollment_date.

3. RIGHT JOIN – Keep ALL Rows from Right Table

RIGHT JOIN keeps all rows from the right table, and matches from the left.

Example – Show ALL courses and the students enrolled in them (even courses with no students)

SQL

Result: All 5 courses appear. “Hindi” has Kavya, “Computer Science” has Aarav, etc. If a course had no enrollments (none in our data), it would still show with NULL student.

4. FULL OUTER JOIN – (Emulated in MySQL)

MySQL does not have FULL OUTER JOIN directly, but we can emulate it using LEFT JOIN + RIGHT JOIN + UNION.

Emulated FULL OUTER JOIN:

SQL

This shows all students + all courses, with NULLs where there is no match.

5. SELF JOIN – Join a Table with Itself

Useful when a table has a hierarchical or comparative relationship.

Example – Find students who share the same birth month

SQL

Result: Pairs of students born in the same month (e.g., Priya and Meera both in February).

6. CROSS JOIN – Cartesian Product

CROSS JOIN combines every row from first table with every row from second table (no ON condition needed).

Syntax:

SQL

Example – All possible student-course combinations (even if not enrolled)

SQL

Result: 50 rows! Every student paired with every course.

When to use: Rare, but useful for generating combinations or testing.

7. Multiple Table Joins – Joining 3+ Tables

We already did this in INNER JOIN example — it’s just chaining more JOINs.

Example – Full report: Student name, course name, fees, enrollment date

SQL

That’s it for Chapter 9! 🎉 You can now connect any related tables and answer complex business questions!

Homework for today (do it right now – very important!) Run these 4 queries and paste the output (at least first 5–6 rows of each):

SQL

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *