What is the Difference Between INNER JOIN
and OUTER JOIN
?
In SQL, INNER JOIN
and OUTER JOIN
are used to combine rows from two or more tables based on a related column. The primary difference lies in how these joins handle unmatched rows.
1. INNER JOIN
The INNER JOIN
returns only the rows that have matching values in both tables. If there is no match, the row is excluded from the result.
Syntax:
SELECT columns
FROM table1
INNER JOIN table2
ON table1.column = table2.column;
Key Characteristics:
- Returns rows where there is a match in both tables.
- Excludes rows with no corresponding match.
Example:
Table: employees
EmployeeID | Name | DepartmentID |
---|---|---|
1 | Alice | 101 |
2 | Bob | 102 |
3 | Charlie | 103 |
Table: departments
DepartmentID | DepartmentName |
---|---|
101 | HR |
102 | IT |
Query:
SELECT employees.Name, departments.DepartmentName
FROM employees
INNER JOIN departments
ON employees.DepartmentID = departments.DepartmentID;
Result:
Name | DepartmentName |
---|---|
Alice | HR |
Bob | IT |
- Only rows with matching
DepartmentID
are included.
2. OUTER JOIN
The OUTER JOIN
includes rows from one or both tables, even if there is no match. There are three types of OUTER JOINs
:
-
LEFT JOIN (or LEFT OUTER JOIN): Returns all rows from the left table, with matching rows from the right table (or
NULL
for unmatched rows). -
RIGHT JOIN (or RIGHT OUTER JOIN): Returns all rows from the right table, with matching rows from the left table (or
NULL
for unmatched rows). -
FULL JOIN (or FULL OUTER JOIN): Returns all rows from both tables, with
NULL
in place of unmatched columns.
2.1 LEFT JOIN
Returns all rows from the left table, even if there is no match in the right table.
Syntax:
SELECT columns
FROM table1
LEFT JOIN table2
ON table1.column = table2.column;
Query:
SELECT employees.Name, departments.DepartmentName
FROM employees
LEFT JOIN departments
ON employees.DepartmentID = departments.DepartmentID;
Result:
Name | DepartmentName |
---|---|
Alice | HR |
Bob | IT |
Charlie | NULL |
- "Charlie" is included even though there is no matching
DepartmentID
.
2.2 RIGHT JOIN
Returns all rows from the right table, even if there is no match in the left table.
Syntax:
SELECT columns
FROM table1
RIGHT JOIN table2
ON table1.column = table2.column;
Query:
SELECT employees.Name, departments.DepartmentName
FROM employees
RIGHT JOIN departments
ON employees.DepartmentID = departments.DepartmentID;
Result
Name | DepartmentName |
---|---|
Alice | HR |
Bob | IT |
NULL | Finance |
- "Finance" is included even though there is no matching employee.
2.3 FULL OUTER JOIN
Returns all rows from both tables. Rows without matches are filled with NULL
.
Syntax:
SELECT columns
FROM table1
FULL OUTER JOIN table2
ON table1.column = table2.column;
Query:
SELECT employees.Name, departments.DepartmentName
FROM employees
FULL OUTER JOIN departments
ON employees.DepartmentID = departments.DepartmentID;
Result:
Name | DepartmentName |
---|---|
Alice | HR |
Bob | IT |
Charlie | NULL |
NULL | Finance |
- Includes all rows from both tables, with
NULL
for non-matching data.
Key Differences
Feature | INNER JOIN | OUTER JOIN |
---|---|---|
Matching Rows | Returns only matching rows. | Returns all rows from one or both tables. |
Unmatched Rows | Excluded from the result. | Included with NULL values for missing columns. |
Performance | Generally faster. | Can be slower due to more data being processed. |
Variants | Single type. | Includes LEFT , RIGHT , and FULL OUTER JOIN . |
Use Cases
INNER JOIN: Use when you need only matching records, such as finding employees working in specific departments.
LEFT JOIN: Use when you need all records from one table, such as listing all employees with or without department assignments.
RIGHT JOIN: Use when you need all records from the second table, such as listing all departments with or without assigned employees.
FULL OUTER JOIN: Use when you need all records from both tables, such as finding mismatched records in data integration.
Conclusion
The choice between INNER JOIN
and OUTER JOIN
depends on the requirements of your query. While INNER JOIN
is efficient for fetching matching records, OUTER JOIN
is ideal when unmatched rows are also important.
Hi, I'm Abhay Singh Kathayat!
I am a full-stack developer with expertise in both front-end and back-end technologies. I work with a variety of programming languages and frameworks to build efficient, scalable, and user-friendly applications.
Feel free to reach out to me at my business email: kaashshorts28@gmail.com.
Top comments (0)