• 文摘: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 left and 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

    Syntax diagram - FULL OUTER JOIN

    Example: SQL FULL OUTER JOIN

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

    Sql full outer sample table image1

    SQL Code:

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

    Output:

    Sql full outer output image1

    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 -

    Sql full outer output image2

    Pictorial Presentation: SQL FULL OUTER JOIN

    Sql full outer join image
    Sql full outer join image
    Sql full outer join image

    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:

    Sql full outer join with left and right outer join

    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;
    

    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;
    

    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:

    Sql full outer sample tables

    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;
    

    Sql full outer join union output

    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 -

    SQL JOINS, slide presentation

    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.

  • 相关阅读:
    如何解决虚拟机频繁分离和附加磁盘导致的识别错误
    创建基于 AFS 的 Docker 容器卷
    使用 docker-machine 管理 Azure 容器虚拟机
    SSH 无法启动的原因分析及解决方法
    Azure 经典模式中虚拟机证书指纹的生成和作用
    远程桌面到 Ubuntu 虚拟机
    Azure Linux 虚拟机常见导致无法远程的操作
    Azure Linux 虚机上配置 RAID 的常见问题及解决方案
    使用 Azure CLI 在 Azure China Cloud 云平台上手动部署一套 Cloud Foundry
    数据库设计(六)第二范式(2NF)?
  • 原文地址:https://www.cnblogs.com/chickenwrap/p/9950010.html
Copyright © 2020-2023  润新知