Home > Database > Mysql Tutorial > INNER JOIN vs OUTER JOIN: Understanding SQL Joins in Depth

INNER JOIN vs OUTER JOIN: Understanding SQL Joins in Depth

DDD
Release: 2024-12-20 20:57:10
Original
660 people have browsed it

INNER JOIN vs OUTER JOIN: Understanding SQL Joins in Depth

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;
Copy after login
Copy after login

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;
Copy after login
Copy after login

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
INNER JOIN table2
ON table1.column = table2.column;
Copy after login
Copy after login

Query:

SELECT employees.Name, departments.DepartmentName
FROM employees
INNER JOIN departments
ON employees.DepartmentID = departments.DepartmentID;
Copy after login
Copy after login

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
LEFT JOIN table2
ON table1.column = table2.column;
Copy after login

Query:

SELECT employees.Name, departments.DepartmentName
FROM employees
LEFT JOIN departments
ON employees.DepartmentID = departments.DepartmentID;
Copy after login

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
RIGHT JOIN table2
ON table1.column = table2.column;
Copy after login

Query:

SELECT employees.Name, departments.DepartmentName
FROM employees
RIGHT JOIN departments
ON employees.DepartmentID = departments.DepartmentID;
Copy after login

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
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.
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.
  1. Use Cases

  2. INNER JOIN

    : Use when you need only matching records, such as finding employees working in specific departments.
  3. LEFT JOIN

    : Use when you need all records from one table, such as listing all employees with or without department assignments.
  4. RIGHT JOIN

    : Use when you need all records from the second table, such as listing all departments with or without assigned employees.

  5. 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.

    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!

    source:dev.to
    Statement of this Website
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
    Popular Tutorials
    More>
    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template