INNER JOIN
in SQL is used to combine rows from two or more tables based on a related column between them. It returns only the rows where there is a match in both tables. If there is no match, the rows are excluded from the result set. This is the most commonly used type of join in SQL.
INNER JOIN
returns only the rows where there is a match in both tables.column1, column2, ...
: The columns you want to retrieve.table1, table2
: The tables you want to join.common_column
: The column that relates the two tables.Employees
and Departments
.
Table: Employees
EmployeeID | Name | City | DepartmentID |
---|---|---|---|
1 | Anand | Chennai | 101 |
2 | Bala | Coimbatore | 102 |
3 | Kavitha | Karaikal | 103 |
4 | Raj | Madurai | 104 |
5 | Kumar | Trichy | 105 |
DepartmentID | DepartmentName |
---|---|
101 | HR |
102 | Finance |
103 | IT |
104 | Marketing |
105 | Sales |
Name | DepartmentName |
---|---|
Anand | HR |
Bala | Finance |
Kavitha | IT |
Raj | Marketing |
Kumar | Sales |
INNER JOIN
with the WHERE
clause to filter the results further. For example, retrieve employees who work in the IT department.
Name | DepartmentName |
---|---|
Kavitha | IT |
INNER JOIN
to join more than two tables. Suppose you have an additional table named Projects
.
Table: Projects
ProjectID | ProjectName | DepartmentID |
---|---|---|
201 | Project A | 101 |
202 | Project B | 102 |
203 | Project C | 103 |
Name | DepartmentName | ProjectName |
---|---|---|
Anand | HR | Project A |
Bala | Finance | Project B |
Kavitha | IT | Project C |
Students
and another table named Courses
.
Table: Students
StudentID | Name | City | CourseID |
---|---|---|---|
1 | Ram | Chennai | 101 |
2 | Karthik | Coimbatore | 102 |
3 | David | Bangalore | 103 |
CourseID | CourseName |
---|---|
101 | Mathematics |
102 | Science |
103 | English |
Name | CourseName |
---|---|
Ram | Mathematics |
Karthik | Science |
David | English |
INNER JOIN
returns only the rows where there is a match in both tables.INNER JOIN
with the WHERE
clause to filter results further.INNER JOIN
can be used to join multiple tables in a single query.