文摘:OUTER JOIN

原文地址:(https://www.w3resource.com/sql/joins/perform-a-full-outer-join.php)

What is Full Outer Join in SQL?

In SQL the FULL OUTER JOIN combines the results of both leftand right outer joins and returns all (matched or unmatched) rows from the tables on both sides of the join clause.

Syntax:

SELECT *
FROM table1
FULL OUTER JOIN table2
ON table1.column_name=table2.column_name;

Syntax diagram - FULL OUTER JOIN

Example: SQL FULL OUTER JOIN

Let’s combine the same two tables using a full join.

SQL Code:

SELECT * FROM table_A
FULL OUTER JOIN table_B
ON table_A.A=table_B.A;

Copy

Output:

Because this is a full join, all rows (both matching and nonmatching) from both tables are included in the output. There is only one match between table table_A and table table_B, so only one row of output displays values in all columns. All remaining rows of output contain only values from table table_A or table table_B, with the remaining columns set to missing values

only one row of output displays values in all columns explain below -

Pictorial Presentation: SQL FULL OUTER JOIN



Example: SQL FULL OUTER JOIN between two tables

Here is an example of full outer join in SQL between two tables.

Sample table: foods

Sample table: company

As we know the FULL OUTER JOIN is the combination of the results of both LEFT OUTER JOIN and RIGHT OUTER JOIN, so, here we are going to describe how FULL OUTER JOIN perform internally.

Pictorial Presentation:

Here is the SQL statement which returns all rows from the ‘foods‘ table and ‘company‘ table using "FULL OUTER JOIN" clause.

SQL Code:

SELECT a.company_id AS "a.ComID",
a.company_name AS "C_Name",
b.company_id AS "b.ComID",
b.item_name AS "I_Name"
FROM   company a
FULL OUTER JOIN foods b
ON a.company_id = b.company_id;

Copy

Output:

a.ComID    C_Name                    b.ComID    I_Name
---------- ------------------------- ---------- -------------
16         Akas Foods                16         Chex Mix
15         Jack Hill Ltd             15         Cheez-It
15         Jack Hill Ltd             15         BN Biscuit
17         Foodies.                  17         Mighty Munch
15         Jack Hill Ltd             15         Pot Rice
18         Order All                 18         Jaffa Cakes
                                                Salt n Shake
19	     sip-n-Bite.

FULL OUTER JOIN using WHERE clause

We can include a WHERE clause with a FULL OUTER JOIN to get return only those rows where no matching data between the joining tables are exist.

The following query returns only those company that have no matching food product in foods, as well as that food product in foods that are not matched to the listed company.


SELECT a.company_id AS "a.ComID",
a.company_name AS "C_Name",
b.company_id AS "b.ComID",
b.item_name AS "I_Name"
FROM   company a
FULL OUTER JOIN foods b
ON a.company_id = b.company_id
WHERE a.company_id IS NULL
OR b.company_id IS NULL
ORDER BY company_name;

Copy

Output:

a.ComID    C_Name                    b.ComID    I_Name
---------- ------------------------- ---------- ---------------
19         sip-n-Bite.
                                                Salt n Shake

FULL OUTER JOIN using UNION clause

A UNION clause can be used as an alternate to get the same result as FULL OUTER JOIN

Here is the example:

Here is the SQL statement:

SELECT table_a.A,table_a.M,table_b.A,table_b.N
FROM table_A
FULL OUTER JOIN table_B
ON table_A.a=table_b.A
ORDER BY table_A.A;

FULL OUTER JOIN using LEFT and RIGHT OUTER JOIN and UNION clause

The following code is, the combination of LEFT OUTER JOIN and RIGHT OUTER JOIN and combined by, using UNION clause


SELECT table_a.A,table_a.M,table_b.A,table_b.N
FROM table_A
LEFT OUTER JOIN table_B
ON table_A.a=table_b.A
UNION
SELECT table_a.A,table_a.M,table_b.A,table_b.N
FROM table_A
RIGHT OUTER JOIN table_B
ON table_A.a=table_b.A;

Copy

Note: Outputs of the said SQL statement shown here is taken by using Oracle Database 10g Express Edition.

Click on the following to get the slides presentation of all JOINS -

Here is a new document which is a collection of questions with short and simple answers, useful for learning SQL as well as for interviews.

原文地址:https://www.cnblogs.com/chickenwrap/p/9950010.html

时间: 2024-11-07 07:25:38

文摘:OUTER JOIN的相关文章

left ,right ,cross ,full/left outer join/区别 详解

--创建测试表wwif OBJECT_ID('qq') is not null drop table qqcreate table qq([序号] varchar(5),[内容1] varchar(10),[内容2] varchar(15))insert into qq([序号],[内容1],[内容2])select 'dd','zoumin','yuzulin'union all select 'cc','zm','yz'union allselect 'AA','z1','yz1'union

Outer Join Query Over Dblink Can Fail With ORA-904 (Doc ID 730256.1)

Outer Join Query Over Dblink Can Fail With ORA-904 (Doc ID 730256.1) To Bottom Modified:03-May-2013Type:PROBLEM In this Document   Symptoms   Changes   Cause   Solution   References APPLIES TO:Oracle Database - Enterprise Edition - Version 10.2.0.1 t

SQL中inner join、outer join和cross join的区别

摘自:http://blog.csdn.net/scythe666/article/details/51881235 缺省情况下是inner join,开发中使用的left join和right join属于outer join,另外outer join还包括full join.下面我通过图标让大家认识它们的区别. 现有两张表,Table A 是左边的表.Table B 是右边的表.其各有四条记录,其中有两条记录name是相同的: 1.INNER JOIN 产生的结果是AB的交集 SELECT

SQL 查询条件放在LEFT OUTER JOIN 的ON语句后与放在WHERE中的区别

这两种条件放置的位置不同很容易让人造成混淆,以致经常查询出莫名其妙的结果出来,特别是副本的条件与主表不匹配时,下面以A,B表为例简单说下我的理解. 首先要明白的是: 跟在ON 后面的条件是对参与左联接的数据进行筛选,即在左联接之前起作用. 跟在WHERE后的条件是对左联接得到的结果集进行筛选,即在左联接之后起作用. 我直接把我的结论发出来,建议朋友们自行测试一下,下面是结论: 1) 如果条件是由主表和副表之间的字段构成,那么放在ON后与放在WHERE子条件中所得到的结果是一样,即这种条件可以随便

inner join和outer join

内连接           只连接匹配的行 左外连接        包含左边表的全部行(不管右边的表中是否存在与它们匹配的行),以及右边表中全部匹配的行 A left join B等价于A left outer join B 右外连接        包含右边表的全部行(不管左边的表中是否存在与它们匹配的行),以及左边表中全部匹配的行 A right join B 全外连接        包含左.右两个表的全部行,不管另外一边的表中是否存在与它们匹配的行.A full outer join B 例

Linq To SQL LEFT OUTER JOIN (Left Join)

SQL: SELECT [t0].[ProductName], [t1].[TotalPrice] AS [TotalPrice] FROM [Product] AS [t0] LEFT OUTER JOIN [OrderDetail] AS [t1] ON [t0].[ProductID] = [t1].[ProductID] Linq Query: from p in Products join od in OrderDetails on p.ProductID equals od.Prod

SQL SERVER 2012 第四章 连接 JOIN の OUTER JOIN,完全连接FULL JOIN,交叉连接CROSS JOIN

SELECT <SELECT LIST> FROM <the table you want to be the "LEFT" table> <LEFT|RIGHT> [OUTER] JOIN <table you want to be the "RIGHT" table> ON <join condition> 可以看做JOIN之前的表是左表,之后的表是右表. 外部连接本质上是包含的.明确包含的记录取决于使

Hibernate中的left outer join

首先,最简单的是一对多的连接,比如: select student from Teacher t join t.students student where student.... 如果是多对一呢?这里有隐式和显示的区别(上面的一对多的情况属于隐式连接).可以像下面这样 select student from Student student where student.teacher.age>30 这属于隐式的,Hibernate会自动连接Teacher表.也可以像下面这样显示的连接: sele

SQL:OUTER JOIN使用方法具体解释

SQL--JOIN使用方法 外联接. 外联接能够是左向外联接.右向外联接或完整外部联接. 在 FROM 子句中指定外联接时,能够由下列几组keyword中的一组指定: LEFT JOIN 或 LEFT OUTER JOIN. 左向外联接的结果集包含 LEFT OUTER 子句中指定的匹配条件的行和左表的全部行. RIGHT JOIN 或 RIGHT OUTER JOIN. 右向外联接是左向外联接的反向联接.将返回匹配条件的行和右表的全部行. FULL JOIN 或 FULL OUTER JOIN