261

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?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Saajid Ismail
  • 8,029
  • 11
  • 48
  • 56

9 Answers9

300

A CROSS JOIN produces a cartesian product between the two tables, returning all possible combinations of all rows. It has no ON clause because you're just joining everything to everything.

A FULL OUTER JOIN is a combination of a LEFT OUTER and RIGHT OUTER join. It returns all rows in both tables that match the query's WHERE clause, and in cases where the ON condition can't be satisfied for those rows it puts NULL values in for the unpopulated fields.

This wikipedia article explains the various types of joins with examples of output given a sample set of tables.

Community
  • 1
  • 1
Donnie
  • 45,732
  • 10
  • 64
  • 86
  • Then in case of big tables `FROM t1 FULL OUTER JOIN t2 ON t1.id=t2.id` will always be faster than `FROM t1,t2 WHERE t1.id=t2.id` ? – alexkovelsky Aug 20 '14 at 08:29
  • 1
    @alexkovelsky inner joins are often faster when there are few matches between the two tables, as use of indexes means it doesn't bother reading all the rows on one of the tables. a full outer join must always read all rows in both tables (or the relevant indexes). In the case where indexes are not enough, or an underlying heap has to be read to output the needed columns then a full outer join is almost always be slower than an inner join. – Andrew Hill Sep 10 '14 at 00:27
  • 1
    Is `outer join` faster or `cross join`? – Shafizadeh May 11 '16 at 13:19
  • 2
    @Shafizadeh - They do different things. – Donnie Jul 14 '16 at 17:29
  • 15
    What if I FULL OUTER JOIN on True? will the result/performance be similar to a CROSS JOIN? – architectonic Nov 11 '16 at 10:41
  • I think a CROSS JOIN is faster than the join equivalent of FULL JOIN t2 ON 1 = 1. – James Holland Jan 25 '21 at 19:28
90

One thing that might not always be obvious to some is that a cross join with an empty table (or result set) results in empty table (M x N; hence M x 0 = 0)

A full outer join will always have rows unless both M and N are 0.

user2292493
  • 909
  • 6
  • 2
46

I'd like to add one important aspect to other answers, which actually explained this topic to me in the best way:

If 2 joined tables contain M and N rows, then cross join will always produce (M x N) rows, but full outer join will produce from MAX(M,N) to (M + N) rows (depending on how many rows actually match "on" predicate).

EDIT:

From logical query processing perspective, CROSS JOIN does indeed always produce M x N rows. What happens with FULL OUTER JOIN is that both left and right tables are "preserved", as if both LEFT and RIGHT join happened. So rows, not satisfying ON predicate, from both left and right tables are added to the result set.

Konstantin
  • 3,817
  • 4
  • 29
  • 39
  • 5
    Are those bounds excluding possible 1-many matches? A full outer join is still capable of producing (M x N) rows. – maxwellb Oct 23 '12 at 19:13
  • 1
    select COUNT_BIG(\*) FROM Traffic t CROSS JOIN Recipient r and SELECT COUNT_BIG(\*) FROM Traffic t FULL JOIN Recipient r ON (1=1) they are the same. – urlreader Sep 20 '13 at 21:02
  • 5
    Yours is the best real answer. Basically: a `cross join` multiples the tables; a `full outer join` adds them in the worst case, depending on how many rows match.. – Brian Peterson Jun 11 '14 at 07:30
  • 5
    This is wrong. FULL JOIN ON rows are INNER JOIN ON rows UNION ALL unmatched left table rows null-extended UNION ALL unmatched right table rows null-extended. So FULL JOIN can return M\*N rows--possibly greater than both MAX(M,N) & M+N. But anyway the min & max number of rows returned as a function of M & N *is just not useful*. What is useful is a *clear definition of FULL JOIN ON*--in terms of INNER JOIN ON & unmatched rows. – philipxy Dec 07 '19 at 03:54
  • 4
    Can you respond to @philipxy's comment? It seems to me that philipxy is correct and that your answer is incorrect. It seems that an outer join (or even an inner join) could have greater than M+N rows (e.g. a join on True would be equivalent to a cross join and produce M*N rows). Maybe your calculation assumes an outer join on columns with unique values (e.g. on primary keys)? Or am I missing something? – igal Jun 29 '20 at 03:09
35

For SQL Server, CROSS JOIN and FULL OUTER JOIN are different. CROSS JOIN is simply Cartesian Product of two tables, irrespective of any filter criteria or any condition.

FULL OUTER JOIN gives unique result set of LEFT OUTER JOIN and RIGHT OUTER JOIN of two tables. It also needs ON clause to map two columns of tables.

Table 1 contains 10 rows and Table 2 contains 20 rows with 5 rows matching on specific columns.

Then CROSS JOIN will return 10*20=200 rows in result set.

FULL OUTER JOIN will return 25 rows in result set. INNER JOIN will return matching rows, hence, 5 rows in result set.

FULL OUTER JOIN (or any other JOIN) always returns result set with less than or equal to Cartesian Product number.

Number of rows returned by FULL OUTER JOIN equal to (No. of Rows by LEFT OUTER JOIN) + (No. of Rows by RIGHT OUTER JOIN) - (No. of Rows by INNER JOIN).

Chirag
  • 4,046
  • 1
  • 33
  • 24
20

They are the same concepts, apart from the NULL value returned.

See below:

declare @table1 table( col1 int, col2 int );
insert into @table1 select 1, 11 union all select 2, 22;

declare @table2 table ( col1 int, col2 int );
insert into @table2 select 10, 101 union all select 2, 202;

select
    t1.*,
    t2.*
from @table1 t1
full outer join @table2 t2 on t1.col1 = t2.col1
order by t1.col1, t2.col1;

/* full outer join
col1        col2        col1        col2
----------- ----------- ----------- -----------
NULL        NULL        10          101
1           11          NULL        NULL
2           22          2           202
*/

select
    t1.*,
    t2.*
from @table1 t1
cross join @table2 t2
order by t1.col1, t2.col1;

/* cross join
col1        col2        col1        col2
----------- ----------- ----------- -----------
1           11          2           202
1           11          10          101
2           22          2           202
2           22          10          101
*/
Protiguous
  • 89
  • 2
  • 9
rio
  • 685
  • 9
  • 16
20

Cross join :Cross Joins produce results that consist of every combination of rows from two or more tables. That means if table A has 3 rows and table B has 2 rows, a CROSS JOIN will result in 6 rows. There is no relationship established between the two tables – you literally just produce every possible combination.

Full outer Join : A FULL OUTER JOIN is neither "left" nor "right"— it's both! It includes all the rows from both of the tables or result sets participating in the JOIN. When no matching rows exist for rows on the "left" side of the JOIN, you see Null values from the result set on the "right." Conversely, when no matching rows exist for rows on the "right" side of the JOIN, you see Null values from the result set on the "left."

KuldipMCA
  • 3,079
  • 7
  • 28
  • 48
7

Cross Join: http://www.dba-oracle.com/t_garmany_9_sql_cross_join.htm

TLDR; Generates a all possible combinations between 2 tables (Carthesian product)

(Full) Outer Join: http://www.w3schools.com/Sql/sql_join_full.asp

TLDR; Returns every row in both tables and also results that have the same values (matches in CONDITION)

Ani Menon
  • 27,209
  • 16
  • 105
  • 126
Sjuul Janssen
  • 1,772
  • 1
  • 14
  • 28
6

SQL FULL OUTER JOIN

  • The FULL OUTER JOIN returns all rows from the left table (table1) and from the right table (table2) irrespective of the match.

  • The FULL OUTER JOIN keyword combines the result of both LEFT OUTER JOIN and RIGHT OUTER JOIN

  • SQL full outer join is also known as FULL JOIN

Reference : http://datasciencemadesimple.com/sql-full-outer-join/

SQL CROSS JOIN

  • In SQL CROSS JOIN Each Row of first table is mapped with the each and every row of second table.

  • Number of rows produced by a result set of CROSS JOIN operation is equal to number of rows in the first table multiplied by the number of rows in the second table.

  • CROSS JOIN is also known as Cartesian product / Cartesian join

  • Number of rows in table A is m, Number of rows in table B is n and resultant table will have m*n rows

Reference:http://datasciencemadesimple.com/sql-cross-join/

karaimadai
  • 101
  • 1
  • 6
4

A full outer join combines a left outer join and a right outer join. The result set returns rows from both tables where the conditions are met but returns null columns where there is no match.

A cross join is a Cartesian product that does not require any condition to join tables. The result set contains rows and columns that are a multiplication of both tables.

Protiguous
  • 89
  • 2
  • 9
Sujeet
  • 91
  • 1
  • 2
  • 6