Home >Database >Mysql Tutorial >INNER JOIN vs OUTER JOIN: Understanding SQL Joins in Depth
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.
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.
SELECT columns FROM table1 INNER JOIN table2 ON table1.column = table2.column;
EmployeeID | Name | DepartmentID |
---|---|---|
1 | Alice | 101 |
2 | Bob | 102 |
3 | Charlie | 103 |
DepartmentID | DepartmentName |
---|---|
101 | HR |
102 | IT |
Query:
SELECT employees.Name, departments.DepartmentName FROM employees INNER JOIN departments ON employees.DepartmentID = departments.DepartmentID;
Name | DepartmentName |
---|---|
Alice | HR |
Bob | IT |
The OUTER JOIN includes rows from one or both tables, even if there is no match. There are three types of OUTER JOINs:
Returns all rows from the left table, even if there is no match in the right table.
Syntax:
SELECT columns FROM table1 INNER JOIN table2 ON table1.column = table2.column;
Query:
SELECT employees.Name, departments.DepartmentName FROM employees INNER JOIN departments ON employees.DepartmentID = departments.DepartmentID;
Name | DepartmentName |
---|---|
Alice | HR |
Bob | IT |
Charlie | NULL |
Returns all rows from the right table, even if there is no match in the left 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;
Name | DepartmentName |
---|---|
Alice | HR |
Bob | IT |
NULL | Finance |
Returns all rows from both tables. Rows without matches are filled with NULL.
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;
Name | DepartmentName |
---|---|
Alice | HR |
Bob | IT |
Charlie | NULL |
NULL | Finance |
Feature | INNER JOIN | OUTER JOIN | |||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
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.
Conclusion
The above is the detailed content of INNER JOIN vs OUTER JOIN: Understanding SQL Joins in Depth. For more information, please follow other related articles on the PHP Chinese website!