A join is used to combine data from two or more tables based on a common column or set of columns. There are several types of joins in SQL, including inner join, left join, right join, and full outer join.
When we work with databases, it is very rare that all information is stored in a single table.
Just like in real life, different pieces of information live in different places—but they are still connected.
For example, student details may be stored in one table and their course details in another.
To bring these related pieces of information together, SQL uses something called a JOIN.
A JOIN in SQL is a way of combining rows from two or more tables based on a related column between them.
It helps us see a complete picture by linking tables that share a relationship.
I often explain this to students with a simple example:
Imagine you have one list with student names and student IDs, and another list with student IDs and their marks.
If you want to see the names along with the marks, you must connect the two lists using the common column (student ID).
That connection is what a JOIN does.
Formal Definition
A JOIN is an SQL operation that retrieves data from multiple tables by matching related columns, usually using a primary key and foreign key.
Why Do We Use JOINs?
JOINs are used when:
- Data is spread across multiple tables
- We want to combine related information
- We want cleaner, normalized database design
- We need meaningful reports or insights
Without JOINs, we would end up repeating data in one large table, which is inefficient and error-prone.
Simple Example
Suppose we have two tables:
Students Table
| StudentID | Name |
|---|---|
| 1 | Aditi |
| 2 | Rohan |
Marks Table
| StudentID | Marks |
|---|---|
| 1 | 88 |
| 2 | 92 |
To display each student’s name with their marks, we write:
SELECT Students.Name, Marks.Marks
FROM Students
JOIN Marks
ON Students.StudentID = Marks.StudentID;
Here, the JOIN connects the two tables using StudentID.
Common Types of JOINs
Although there are several, the main ones you will use are:
- INNER JOIN – returns only the matching rows
- LEFT JOIN – returns all rows from the left table and matching rows from the right
- RIGHT JOIN – returns all rows from the right table and matching rows from the left
- FULL JOIN – returns all rows from both tables
- CROSS JOIN – returns every combination of rows
Each type helps answer a different kind of question.