📜  SQL中内连接和外连接的区别

📅  最后修改于: 2021-09-15 01:08:24             🧑  作者: Mango

1. 内连接:
它是 SQL 中的一种连接操作。内连接是一种操作,它返回两个或多个表之间的组合元组,其中至少有一个属性是共同的。如果表之间没有共同的属性,则它将不返回任何内容。句法:

select * 
from table1 INNER JOIN table2
on table1.column_name = table2.column_name;

或者

select *
from table1 JOIN table2
on table1.column_name = table2.column_name;

2. 外连接:
它是 SQL 中的一种 Join 操作。外连接是一种即使连接条件失败也会从指定表返回组合元组的操作。 SQL中有三种类型的外连接,即

  1. 左外连接
  2. 右外连接
  3. 全外连接

左外连接的语法:

select *
from table1 LEFT OUTER JOIN table2
on table1.column_name = table2.column_name;

右外连接的语法:

select *
from table1 RIGHT OUTER JOIN table2
on table1.column_name = table2.column_name;

全外连接的语法:

select *
from table1 FULL OUTER JOIN table2
on table1.column_name = table2.column_name;

下面是 INNER JOINOUTER JOIN之间的区别表:

S.No Inner Join Outer Join
1. It returns the combined tuple between two or more tables. It returns the combined tuple from a specified table even join condition will fail.
2. Used clause INNER JOIN and JOIN. Used clause LEFT OUTER JOIN, RIGHT OUTER JOIN, FULL OUTER JOIN, etc.
3. When any attributes are not common then it will return nothing. It does not depend upon the common attributes. If the attribute is blank then here already placed NULL.
4. If tuples are more. Then INNER JOIN works faster than OUTER JOIN. Generally, The OUTER JOIN is slower than INNER JOIN. But except for some special cases.
5. It is used when we want detailed information about any specific attribute. It is used when we want to complete information.
6. JOIN and INNER JOIN both clauses work the same. FULL OUTER JOIN and FULL JOIN both clauses work the same.
7. SQL Syntax:
select *
from table1 INNER JOIN / JOIN table2
ON table1.column_name = table2.column_name;
SQL Syntax:
select *
from table1 LEFT OUTER JOIN / RIGHT OUTER JOIN /
FULL OUTER JOIN / FULL JOIN table2 ON
table1.column_name = table2.column_name;