SQL Server: What is the difference between CROSS JOIN and FULL OUTER JOIN?

前端 未结 10 1561
星月不相逢
星月不相逢 2020-11-28 18:38

What is the difference between CROSS JOIN and FULL OUTER JOIN in SQL Server?

Are they the same, or not? Please explain. When would one use either of these?

10条回答
  •  囚心锁ツ
    2020-11-28 19:13

    Here is an example where both the FULL OUTER JOIN and CROSS JOIN return the same result set without NULL returned. Please note the 1 = 1 in the ON clause for the FULL OUTER JOIN:

    declare @table1 table (     col1    int,    col2    int ) 
    declare @table2 table (     col1    int,    col2    int )
    
    insert into @table1 select  1, 11   union all select    2, 22   
    
    insert into @table2 select  10, 101 union all select     2, 202
    
    select  *
    from    @table1 t1 full outer join @table2 t2
        on  1 = 1
    
    (2 row(s) affected)
    
    (2 row(s) affected)
    col1        col2        col1        col2
    ----------- ----------- ----------- -----------
    1           11          10          101
    2           22          10          101
    1           11          2           202
    2           22          2           202
    
    select  *
    from    @table1 t1 cross join @table2 t2
    
    col1        col2        col1        col2
    ----------- ----------- ----------- -----------
    1           11          10          101
    2           22          10          101
    1           11          2           202
    2           22          2           202
    
    (4 row(s) affected)
    

提交回复
热议问题