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
3
votes
2 answers

R data.table cross-join by three variables

I'm trying cross join a data.table by three variables (group, id, and date). The R code below accomplishes exactly what I want to do, i.e., each id within each group is expanded to include all of the dates_wanted. But is there a way to do the same…
user1491868
  • 596
  • 4
  • 15
  • 42
3
votes
2 answers

R: cross join column on itself excluding symmetric results

I have a data table init that looks like this: > init +---+ | id| +---+ | a| +---+ | b| +---+ | c| +---+ I want to obtain all pairs for id column, so I need to cross join the init data table on itself. Additionally, I want to exclude…
red_quark
  • 971
  • 5
  • 20
3
votes
1 answer

PySpark cross join excluding symmetric results

I have a Spark dataframe: > df +---+ | id| +---+ | a| +---+ | b| +---+ | c| +---+ I want to obtain all pairs for id column, so I need to cross join the dataframe on itself. But I want to exclude symmetric results (in my case a,b == b,a,…
red_quark
  • 971
  • 5
  • 20
3
votes
1 answer

Insert multiple values with the same foreign key

I have two tables that reference each other: CREATE TABLE Room room_id INTEGER PRIMARY KEY, room_name TEXT UNIQUE NOT NULL; CREATE TABLE Item item_id INTEGER PRIMARY KEY, room_id INTEGER, item_name TEXT, FOREIGN KEY…
Mad Physicist
  • 107,652
  • 25
  • 181
  • 264
3
votes
2 answers

PySpark generate missing dates and fill data with previous value

I need help for this case to fill, with a new row, missing values: This is just an example, but I have a lot of rows with different IDs. Input…
3
votes
5 answers

Super slow query with CROSS JOIN

I have two tables named table_1 (1GB) and reference (250Mb). When I query a cross join on reference it takes 16hours to update table_1 .. We changed the system files EXT3 for XFS but still it's taking 16hrs.. WHAT AM I DOING WRONG?? Here is the…
madkitty
  • 1,657
  • 6
  • 24
  • 37
3
votes
1 answer

How can I cross join dynamically in Laravel?

I want to create product variations like this image: I have tried with static data it works. $collection = collect(["XL", "XXL"]); return $collection->crossJoin(["1kg", "2kg"], ["Red", "Green"]); But I want to create this dynamically. I have tried…
Shahadat Hossain
  • 155
  • 4
  • 12
3
votes
3 answers

Weird Oracle behaviour - cross join in recursive CTE works with comma but not with cross join clause

Suppose following simple recursive query r to list several numbers. When recursive part of query is cross joined with unnecessary 1-row table using old way with comma separation, everything works fine (dbfiddle): with r (x) as ( select 1 as x from…
Tomáš Záluský
  • 10,735
  • 2
  • 36
  • 64
3
votes
2 answers

How to create a MySQL view

I have MySQL visits table as visit_date client_id 2019-01-01 08-00-00 1 2019-01-01 11-00-00 2 work_hours table as hour name 8 '08-00' 9 '09-00' ... 18 '18-00' and tables clients as client_id client_name 1 …
harp1814
  • 1,494
  • 3
  • 13
  • 31
3
votes
2 answers

Cross Join in Apache Spark with dataset is very slow

I have posted this question on spark user forum but received no response so asking it here again. We have a use case where we need to do a Cartesian join and for some reason we are not able to get it work with Dataset API's. We have two…
Ankur
  • 269
  • 1
  • 3
  • 9
3
votes
3 answers

BigQuery argmax: Is array order maintained when doing CROSS JOIN UNNEST

Question: In BigQuery, standard SQL, if I run SELECT * FROM mytable CROSS JOIN UNNEST(mytable.array) Can I be certain that the resulting row order is the same as the array order? Example: Let's say I have the following table mytable: Row | id |…
dlebech
  • 1,817
  • 14
  • 27
3
votes
2 answers

What is the difference between CROSS JOIN and multiple tables in one FROM?

What is the difference? SELECT a.name, b.name FROM a, b; SELECT a.name, b.name FROM a CROSS JOIN b; If there is no difference then why do both exist?
AturSams
  • 7,568
  • 18
  • 64
  • 98
3
votes
1 answer

How avoid cross join in hive?

I have two tables. one includes 1 million records, the other includes 20 million records. table 1 value (1, 1) (2, 2) (3, 3) (4, 4) (5, 4) .... table 2 value (55, 11) (33, 22) (44, 66) (22,…
vito yan
  • 135
  • 1
  • 9
3
votes
2 answers

Eliminating a Cross Join in SQL

I am trying to optimize a query that contains a cross join. I have large query that I proceed to cross join with a derived table. Would it improve the speed of the query by turning the derived table into a view? Or even capturing that information…
Spencer
  • 21,348
  • 34
  • 85
  • 121
3
votes
1 answer

How to do cross join between two H2OFrame in R?

I want to do cross join between two H2OFrames. Looking for work around Strictly in H2OFrame col1.1 <- c('A', 'B', 'E', 'C', 'F', 'D') dummy <- rep(1,6) d1.hex <- as.h2o( cbind( col1.1, dummy ) ) col2.1 <- c('xx', 'yy', 'zz', 'ww') dummy <-…
Vikash Kumar
  • 348
  • 1
  • 9