Questions tagged [cross-join]

CROSS JOIN is a join operation, that returns the Cartesian product of rows from tables in the join. In other words, it will produce rows which combine each row from the first table with each row from the second table. Unlike other JOIN operators, it does not let you specify a join clause. You may, however, specify a WHERE clause in the SELECT statement.

CROSS JOIN is a JOIN operation that produces the Cartesian product of two tables. Unlike other JOIN operators, it does not let you specify a join clause. You may, however, specify a WHERE clause in the SELECT statement.

Examples

The following SELECT statements are equivalent:

SELECT *
FROM TEACHER
CROSS JOIN COURSE

SELECT *
FROM TEACHER, COURSE;

The following SELECT statements are equivalent:

SELECT *
FROM TEACHER
CROSS JOIN COURSE
WHERE TEACHER.ID = COURSE.TEACHER_ID

SELECT *
FROM TEACHER
INNER JOIN COURSE
ON TEACHER.ID = COURSE.TEACHER_ID

A CROSS JOIN operation can be replaced with an INNER JOIN where the join clause always evaluates to true (for example, 1=1). It can also be replaced with a sub-query. So equivalent queries would be:

SELECT * FROM TEACHER LEFT OUTER JOIN
COURSE INNER JOIN UNIVERSITY ON 1=1
ON TEACHER.ID = UNIVERSITIES.TEACHER_ID
WHERE UNIVERSITY.COUNTRY = 'US'

SELECT FROM TEACHER LEFT OUTER JOIN
(SELECT FROM COURSE, UNIVERSITY) S
ON TEACHER.ID = S.TEACHER_ID
WHERE S.COUNTRY = 'US'

Links

Cross Join in Oracle

530 questions
261
votes
9 answers

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

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?
Saajid Ismail
  • 8,029
  • 11
  • 48
  • 56
196
votes
11 answers

CROSS JOIN vs INNER JOIN in SQL

What is the difference between CROSS JOIN and INNER JOIN? CROSS JOIN: SELECT Movies.CustomerID, Movies.Movie, Customers.Age, Customers.Gender, Customers.[Education Level], Customers.[Internet Connection], Customers.[Marital Status],…
PriceCheaperton
  • 5,071
  • 17
  • 52
  • 94
170
votes
13 answers

cartesian product in pandas

I have two pandas dataframes: from pandas import DataFrame df1 = DataFrame({'col1':[1,2],'col2':[3,4]}) df2 = DataFrame({'col3':[5,6]}) What is the best practice to get their cartesian product (of course without writing it explicitly like…
Idok
  • 3,642
  • 4
  • 21
  • 18
136
votes
5 answers

How do you perform a CROSS JOIN with LINQ to SQL?

How do you perform a CROSS JOIN with LINQ to SQL?
Luke Smith
  • 23,504
  • 8
  • 29
  • 28
45
votes
11 answers

How to do cross join in R?

How can I achieve a cross join in R ? I know that "merge" can do inner join, outer join. But I do not know how to achieve a cross join in R. Thanks
zjffdu
  • 25,496
  • 45
  • 109
  • 159
36
votes
8 answers

Generate all possible combinations for Columns(cross join or Cartesian product)

I have a Google SpreadSheets doc with three columns A, B and C. I need to populate the Column C with all the possible combinations of the values in Columns A and B. Please take a look a the capture to see what I mean. I found this to be done in…
JPashs
  • 13,044
  • 10
  • 42
  • 65
35
votes
2 answers

Is CROSS JOIN a synonym for INNER JOIN without ON clause?

I am wondering whether CROSS JOIN can be safely replaced with INNER JOIN in any query when it is found. Is an INNER JOIN without ON or USING exactly the same as CROSS JOIN? If yes, has the CROSS JOIN type been invented only to express intent better…
Benoit
  • 76,634
  • 23
  • 210
  • 236
29
votes
7 answers

SQL INNER JOIN syntax

the two bits of SQL below get the same result SELECT c.name, o.product FROM customer c, order o WHERE c.id = o.cust_id AND o.value = 150 SELECT c.name, o.product FROM customer c INNER JOIN order o on c.id = o.cust_id WHERE o.value =…
Dean Madden
  • 341
  • 3
  • 6
  • 14
25
votes
1 answer

combinations (not permutations) from cross join in sql

If I have a table that I'd like to cross join to itself, how can I remove the duplicate rows? Or to put it another way, how can I do a "order doesn't matter" cross join? So for example, if I have a table T: field | ------- A | B | C …
Ramy
  • 20,541
  • 41
  • 103
  • 153
23
votes
7 answers

Is there a way to perform a cross join or Cartesian product in excel?

At the moment, I cannot use a typical database so am using excel temporarily. Any ideas? The
user1248831
  • 351
  • 1
  • 2
  • 5
22
votes
3 answers

Natural join if no common attributes

What will natural join return in relational algebra if tables don't have attributes with same names? Will it be null or the same as cross join (cross product) (Cartesian product)?
21
votes
1 answer

Why does Hibernate generate a CROSS JOIN for an implicit join of a @ManyToOne association?

Baur & King said in their book: Implicit joins are always directed along many-to-one or one-to-one association, never through a collection-valued association. [P 646, Ch 14] But when I am doing that in the code it is generating a CROSS JOIN…
anirban
  • 674
  • 1
  • 7
  • 21
20
votes
3 answers

How to generate a matrix of combinations

I have 5 items each of which can take on the value of 1 or -1. I want to generate a matrix that consists of rows of the possible combinations. The order of the items does not matter and the order of the combinations does not matter. I know I could…
ProbablePattern
  • 703
  • 2
  • 7
  • 17
19
votes
2 answers

error : subquery must return only one column

I am getting the error subquery must return only one column when I try to run the following query: SELECT mat.mat as mat1, sum(stx.total ) as sumtotal1, ( SELECT mat.mat as mat, sum(stx.total) as sumtotal FROM stx LEFT JOIN mat ON…
user2431581
  • 305
  • 1
  • 2
  • 8
14
votes
2 answers

How to implement "Cross Join" in Spark?

We plan to move Apache Pig code to the new Spark platform. Pig has a "Bag/Tuple/Field" concept and behaves similarly to a relational database. Pig provides support for CROSS/INNER/OUTER joins. For CROSS JOIN, we can use alias = CROSS alias, alias [,…
Shawn Guo
  • 3,169
  • 3
  • 21
  • 28
1
2 3
35 36