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_id | name | dept_id |
---|---|---|
1 | Alice | 10 |
2 | Bob | 20 |
3 | Charlie | 30 |
Departments Table
dept_id | department_name |
---|---|
10 | HR |
20 | IT |
SQL Query for INNER JOIN:
sqlCopyEditSELECT Employees.name, Departments.department_name
FROM Employees
INNER JOIN Departments ON Employees.dept_id = Departments.dept_id;
Output:
name | department_name |
---|---|
Alice | HR |
Bob | IT |
(Charlie is excluded since there is no matching dept_id in the Departments table.) |
Leave a Reply