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
6
votes
4 answers

Are Mondrian / OLAP the wrong tool for joining large dimensions/sets?

Summary: Most of the examples I've seen of MDX joins have involved joining relatively small sets, say with tens or hundreds of items each. But I find myself also wanting to try joining (in particular "non-empty joining") sets that have thousands or…
Chris
  • 9,986
  • 8
  • 48
  • 56
5
votes
1 answer

How to set default value from mysql join interval yearmonth

I have problem with my query. I have two tables and I want join them to get the results based on primary key on first table, but I missing 1 data from first table. this my fiddle as you can see, I missing "xx3" from month 1 I have tried to change…
AdityaDees
  • 1,022
  • 18
  • 39
5
votes
3 answers

Cross join of unknown number of string arrays using linq

Is it possible to do a cross join using linq where the number of joins is not known in advance? I have this: var arrays = new List(); If I know I have three lists I can do: var oQuery = from x in arrays[0] from y in arrays[1] from…
Asken
  • 7,679
  • 10
  • 45
  • 77
5
votes
3 answers

Difference between cross product (cross join, Cartesian product) and natural join

While writing in SQL, how would I know if I should use cross product (cross join, Cartesian product) or natural join?
AamKhayega
  • 89
  • 1
  • 1
  • 3
5
votes
4 answers

PostgreSQL LEFT OUTER JOIN query syntax

Lets say I have a table1: id name ------------- 1 "one" 2 "two" 3 "three" And a table2 with a foreign key to the first: id tbl1_fk option value ------------------------------- 1 1 1 1 2 …
kosta
  • 296
  • 2
  • 6
  • 15
4
votes
3 answers

Google sheets - cross join / cartesian join from two separate columns

Hope that someone can help me with a cartesian product in Google Sheets. I have data in two separate columns and wish to create all possible combinations of the two columns in a separate tab. The first column is ID (text) and the second is date…
4
votes
1 answer

Cross join in Data.table doesnt seem to retain column names

data.table documentation says this, see ?CJ: x = c(1,1,2) y = c(4,6,4) CJ(x, y) # output columns are automatically named 'x' and 'y' However when I run the example, it doesnt seem to be retained x = c(1,1,2) y = c(4,6,4) CJ(x, y) V1 V2 1: 1 …
ashleych
  • 1,042
  • 8
  • 25
4
votes
1 answer

inner join with multiple conditions r data table

I am trying to do an inner join using data table that has multiple, fairly dynamic conditions. I am getting tripped up on the syntax. First, I create two objects, x and x2 that I want to do an inner join with. set.seed(1) #generate data x =…
wolfsatthedoor
  • 7,163
  • 18
  • 46
  • 90
4
votes
2 answers

Why does Hive warn that this subquery would cause a Cartesian product?

According to Hive's documentation it supports NOT IN subqueries in a WHERE clause, provided that the subquery is an uncorrelated subquery (does not reference columns from the main query). However, when I attempt to run the trivial query below, I get…
levand
  • 8,440
  • 3
  • 41
  • 54
4
votes
1 answer

How to use generate_series() to generate a grid of values

I would like to generate a grid of (row,column) pairs, like: 1 | 1 1 | 2 1 | 3 ... 2 | 1 2 | 2 ... My naive approach has this puzzling behaviour: select generate_series(1,5), generate_series(1, 5); generate_series |…
Steve Bennett
  • 114,604
  • 39
  • 168
  • 219
4
votes
3 answers

R data.table: (dynamic) forward looking Cross-Joins

I was wondering if there is an option for the CJ() method in data.table to take vectors formed by an evaluated condition instead of running the full cross join. Data library(data.table) df<-data.table( ID=c(18L, 18L, 18L, 46L, 74L, 74L, 165L,…
Daedalus
  • 235
  • 1
  • 2
  • 9
4
votes
1 answer

Joining two table entities with @OneToOne annotation generate "cross join" while "inner join" expected

I have OneToOne tables/entities Person and Employee: each employee has only one person and each person is attached to one and only one employee. The generated query do tables join with "cross join" keyword while "inner join" would be more…
devware33
  • 81
  • 1
  • 3
4
votes
3 answers

What does this MySQL statement do?

INSERT IGNORE INTO `PREFIX_tab_lang` (`id_tab`, `id_lang`, `name`) (SELECT `id_tab`, id_lang, (SELECT tl.`name` FROM `PREFIX_tab_lang` tl WHERE tl.`id_lang` = (SELECT c.`value` FROM `PREFIX_configuration` c …
user198729
  • 61,774
  • 108
  • 250
  • 348
4
votes
1 answer

DAX / PowerPivot query functions to spread aggregated values over time period

I’m trying to work out the DAX expression [for MS PowerPivot in Excel 2010] to evenly distribute the sum of a value across the range it’s applied to, and re-sum that up for a given time span/period. It’s trivial to cross-apply in SQL server, though…
iivel
  • 2,576
  • 22
  • 19
3
votes
2 answers

SQL Server 2008 using SUM() OVER(ORDER BY...)

I am trying to use a CTE and CROSS JOIN the result set. I want to sum up the 4 rows leading up to the current row. The example online I found does not use a CTE, only a newly created table…
KKlucznik
  • 189
  • 1
  • 3
  • 16
1 2
3
35 36