"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > Detailed explanation of the differences between internal and external SQL connections

Detailed explanation of the differences between internal and external SQL connections

Posted on 2025-04-13
Browse:276

What are the Differences Between Inner, Left, Right, and Outer SQL Joins?

In-depth understanding of the nuances of SQL JOIN

SQL JOIN plays a crucial role in data processing and aggregation. Among various JOIN types, internal JOIN (INNER JOIN), left outer JOIN (LEFT OUTER JOIN), right outer JOIN (RIGHT OUTER JOIN), and full outer JOIN (FULL OUTER JOIN) are often used to combine data from multiple tables according to specific conditions.

Internal JOIN (INNER JOIN)

Internal JOIN only retrieves rows that match the join columns in two or more tables. It contains only rows with matches in both tables, effectively filtering out any mismatched rows.

External JOIN (OUTER JOIN)

When you want to include all rows from one table, use an external JOIN regardless of whether they match the rows in another table. There are three types of external JOIN:

  • Left outer JOIN (LEFT OUTER JOIN): Contains all rows of the left table and any matching rows in the right table. The mismatched rows in the left table will be filled with NULL values.
  • Right outer JOIN (RIGHT OUTER JOIN): Similar to the left outer JOIN, but contains all rows of the right table and any matching rows in the left table. The mismatched rows in the table on the right will be filled with NULL values.
  • Full external JOIN (FULL OUTER JOIN): Contains all rows from the left and right tables. If one party has a match, the matching row is displayed; if there is no match, it is filled with a NULL value.

Example

Consider the examples provided in the article:

Table 1 (Customers)Table 2 (Orders)IDNameIDOrder Date John2023-01-01212023-01-02] 322023-01-03432023-01-04
Customer ID
1] 11
2Mary
3Tom]
Alice4

]] Customer IDName1]1
]Internal JOIN will only return rows where Customer ID matches both tables:
Order Date
John2023-01-01
John

]2023-01-02

What are the Differences Between Inner, Left, Right, and Outer SQL Joins?

Performance Precautions

JOIN's performance depends on various factors, including the size and complexity of the table, the query optimizer used, and the database engine. In general, internal JOINs are more efficient because they filter out mismatched rows. External JOINs can be more resource-consuming, especially when large tables are processed with few matches. Choosing the right JOIN type is essential for optimizing database queries.

Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3