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
12
votes
8 answers

sql cross join - what use has anyone found for it?

Today, for the first time in 10 years of development with sql server I used a cross join in a production query. I needed to pad a result set to a report and found that a cross join between two tables with a creative where clause was a good solution.…
Booji Boy
  • 4,522
  • 4
  • 40
  • 45
12
votes
3 answers

What is the expected behaviour for multiple set-returning functions in SELECT clause?

I'm trying to get a "cross join" with the result of two set-returning functions, but in some cases I don't get the "cross join", see example Behaviour 1: When set lenghts are the same, it matches item by item from each set postgres=# SELECT…
12
votes
2 answers

Why is this Cross Join so Slow in Linq?

I wrote this piece of Linq to handle doing a CROSS Join just like a database would between multiple lists. But for some reason it's extremely slow when any of the lists go more than 3000. I'd wait for 30s ? These lists could go to very large…
Johann du Toit
  • 2,609
  • 2
  • 16
  • 31
10
votes
3 answers

In SQL, what's the difference between JOIN and CROSS JOIN?

What's the difference between: select t1.a1, t1.a2, t1.a3 from t1 cross join t2 where t1.a3 = t2.a1 and: select t1.a1, t1.a2, t1.a3 from t1,t2 where t1.a3=t2.a1; Can I use them interchangeably?
Jaroszewski Piotr
  • 353
  • 1
  • 3
  • 11
9
votes
5 answers

Optimize Spark job that has to calculate each to each entry similarity and output top N similar items for each

I have a Spark job that needs to compute movie content-based similarities. There are 46k movies. Each movie is represented by a set of SparseVectors (each vector is a feature vector for one of the movie's fields such as Title, Plot, Genres, Actors,…
9
votes
5 answers

How to combine two unrelated tables in Mysql

There are two tables that are not related to each other(No foreign keys). How to show them together in MySQL? TABLE1 TABLE2 Result
Seehyung Lee
  • 590
  • 1
  • 15
  • 32
9
votes
2 answers

Self cross-join in pig is disregarded

If one have data like those: A = LOAD 'data' AS (a1:int,a2:int,a3:int); DUMP A; (1,2,3) (4,2,1) And then a cross-join is done on A, A: B = CROSS A, A; DUMP B; (1,2,3) (4,2,1) Why is second A optimized out from the query? info: pig version…
Artem Oboturov
  • 4,344
  • 2
  • 30
  • 48
8
votes
3 answers

Mixing implicit and explicit JOINs

I am having a problem with Hibernate generating invalid SQL. Specifically, mixing and matching implicit and explicit joins. This seems to be an open bug. However, I'm not sure why this is invalid SQL. I have come up with a small toy example that…
Chase Seibert
  • 15,703
  • 8
  • 51
  • 58
8
votes
2 answers

Are data tables with more than 2^31 rows supported in R with the data table package yet?

I am trying to do a cross join (from the original question here), and I have 500GB of ram. The problem is that the final data.table has more than 2^31 rows, so I get this error: Error in vecseq(f__, len__, if (allow.cartesian || notjoin ||…
wolfsatthedoor
  • 7,163
  • 18
  • 46
  • 90
8
votes
1 answer

Why do CROSS JOIN conditions not work in the 'ON' clause, only the WHERE clause?

I'm wondering why a conditional cross join must have the condition(s) specified in the WHERE clause, and why it doesn't work in the 'ON' clause. See link for compiled example: http://rextester.com/IKY8693 Business context: I need to generate a list…
psrpsrpsr
  • 457
  • 1
  • 4
  • 12
8
votes
1 answer

Memory efficient cartesian join in PySpark

I have a large dataset of string ids, that can fit into memory on a single node in my spark cluster. The issue is that it consumes most of the memory for a single node. These ids are about 30 characters long. For…
mgoldwasser
  • 14,558
  • 15
  • 79
  • 103
8
votes
0 answers

Cross Join When Building Predicate for QueryDSL

I am using Query DSL with Spring Data Jpa with QueryDslPredicateExecutor I have the following Entities; @Table(name="a_table") @Entity public class A { @OneToOne @JoinColumn(name = "b_id") private B…
shazin
  • 21,379
  • 3
  • 54
  • 71
7
votes
2 answers

mysql cross join, but without duplicated pair?

Let's say I have the following row in my table table rows id 63 64 65 66 67 68 if I run the following query, I get 30 rows. SELECT r1.id, r2,id FROM rows AS r1 CROSS JOIN rows AS r2 WHERE r1.id!=r2.id result: 63 64 65 64 66 64 67 …
Moon
  • 22,195
  • 68
  • 188
  • 269
7
votes
2 answers

Cross join behaviour (SQLServer 2008)

I have been trying to track down a problem with a query I have. The query is actually generated by hibernate from HQL but the resulting SQL doesn't do what I expect. Modifying the SQL slightly produces the correct result but I'm not sure why the…
Mike Q
  • 22,839
  • 20
  • 87
  • 129
7
votes
3 answers

Add missing rows to data.table according to multiple keyed columns

I have a data.table object that contains multiple columns that specify unique cases. In the small example below, the variables "name", "job", and "sex" specify the unique IDs. I would like to add missing rows so that each each case has a row for…
itpetersen
  • 1,475
  • 3
  • 13
  • 32
1
2
3
35 36