Joins are essential in relational databases because data is stored in separate normalized tables, and queries often require combining these tables to produce meaningful results.
SQL supports several types of joins:
INNER JOIN
LEFT OUTER JOIN
RIGHT OUTER JOIN
FULL OUTER JOIN
CROSS JOIN
SELF JOIN
Let’s understand each in detail with examples.
Returns only the matching rows from both tables.
Student Table
| Sid | Name |
|---|---|
| 1 | Rahul |
| 2 | Neha |
| 3 | Amit |
Course Table
| Sid | Course |
|---|---|
| 1 | BCA |
| 3 | MCA |
| Name | Course |
|---|---|
| Rahul | BCA |
| Amit | MCA |
Only matching rows are returned.
Returns all rows from the left table, and matched rows from the right table.
Non-matching rows from the right table become NULL.
From the previous tables:
| Name | Course |
|---|---|
| Rahul | BCA |
| Neha | NULL |
| Amit | MCA |
Neha has no matching course, so NULL appears.
Returns all rows from the right table, and matched rows from the left table.
(Using Student and Course tables)
| Name | Course |
|---|---|
| Rahul | BCA |
| Amit | MCA |
If any course exists without a student, it would appear with NULL values from Student.
Returns all rows from both tables, with NULL where no match exists.
(Some DBMS like MySQL don’t support FULL OUTER JOIN directly.)
| Name | Course |
|---|---|
| Rahul | BCA |
| Neha | NULL |
| Amit | MCA |
| NULL | MBA |
(This would happen if a course existed without a student.)
Produces the Cartesian product of two tables.
Rows = rows in table1 × rows in table2.
If Student has 3 rows and Course has 2 rows → Output = 6 rows.
Self join means joining a table with itself.
Finding employees and their managers in the same table.
| Join Type | Returns |
|---|---|
| INNER JOIN | Only matching rows |
| LEFT JOIN | All left + matched right |
| RIGHT JOIN | All right + matched left |
| FULL JOIN | All rows from both tables |
| CROSS JOIN | Cartesian product |
| SELF JOIN | Table joined with itself |
SQL Joins enable combining data from multiple related tables, making relational databases powerful and efficient. INNER JOIN is most commonly used, while LEFT JOIN is useful when unmatched rows must be included. FULL JOIN, CROSS JOIN, and SELF JOIN serve special use cases. Understanding joins is essential for writing complex SQL queries.
Take quizzes related to this topic and see where you stand!
Start Quiz Now