Inner Join

An INNER JOIN in SQL returns only the rows where there is a match in both tables based on a specified condition.

Explanation:

  • When two tables are joined using INNER JOIN, only the matching records are included in the result.
  • If a record in one table doesnโ€™t have a corresponding record in the other, it is excluded from the result set.
  • Inner joins are commonly used in relational databases to combine data from multiple tables.

Example:

Consider two tables:
Employees Table

emp_idnamedept_id
1Alice10
2Bob20
3Charlie30

Departments Table

dept_iddepartment_name
10HR
20IT

SQL Query for INNER JOIN:

sqlCopyEditSELECT Employees.name, Departments.department_name
FROM Employees
INNER JOIN Departments ON Employees.dept_id = Departments.dept_id;

Output:

namedepartment_name
AliceHR
BobIT
(Charlie is excluded since there is no matching dept_id in the Departments table.)