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

Efficient cross join with aggregation and filter

As per the title, I am looking to do a cross join with a table which performs an aggregation function and filters on a couple of variables within the table. I have similar data to the…
SlyGrogger
  • 317
  • 5
  • 16
3
votes
2 answers

Why does SQL Management Studio add a cross join?

I sometimes need to run basic updates on a join. For example: UPDATE t1 SET col1 = 'val1' FROM table1 as t1 INNER JOIN table2 as t2 ON t1.ID = t2.t1_id WHERE t2.col3 = 'val3' This works perfectly, but for some reason, in MS SQL Management Studio…
Martijn
  • 11,964
  • 12
  • 50
  • 96
3
votes
1 answer

Pass a list of columns to data.table’s CJ as a vector

I have the following code: main_cols <- c('num', 'let') dt <- data.table(num = 1:5, let = letters[1:5]) dt new_dt <- dt[CJ(num = num , let = let , unique = TRUE) , on = main_cols …
Anarcho-Chossid
  • 2,210
  • 4
  • 27
  • 44
3
votes
1 answer

Generating a set of permutations in SQL Server without reverse duplicates

Table Code: Col1 ---- A1 A2 A3 B1 B2 C1 D1 D2 (I have other columns as well) I am trying to create every possible combination EXCLUDING itself (i.e COL1:A1 COL2:A1) EXCLUDING havING it again the reverse way (i.e A1 A2, A2,A1)... They are to be in…
MM92
  • 33
  • 4
3
votes
1 answer

MySQL numbering records by group - did I hit a bug?

I am trying to number some records in MySQL (5.5.44-0 on Ubuntu), grouped by another column (you will see what I mean below). I am adapting the solution described at Running Sums for Multiple Categories in MySQL, except I'm just numbering, not…
Johann Tagle
  • 106
  • 6
3
votes
2 answers

How to programmatically generate a dataset object from the Cartesian product (aka "cross-join") of multiple one-dimensional cell arrays?

I have n cell arrays c1,c2,…,cn, having dimensions L1 × 1,L2 × 1,…, Ln × 1, respectively. (FWIW, each cell array contains elements of a unique class, but this class may not be the same for all the arrays.) I want to produce a dataset object…
kjo
  • 33,683
  • 52
  • 148
  • 265
3
votes
1 answer

Unsold product query without a cross join?

I'm working on a stored procedure for "unsold products". Here's a summary of the approach I've taken so far. Note: in production there will be a max 7 products and the sales table is has approximately 18,000 growing slowly relative to current…
Aaron Anodide
  • 16,906
  • 15
  • 62
  • 121
3
votes
1 answer

Add blank columns in MDX query

How to add blank columns in MDX Query? I have tried all possible options such as adding member and all, but when I am trying to cross join the new member, i get MDX errors. Please guide me through this. Thanks We need following MDX format and need…
Anand
  • 61
  • 1
  • 6
3
votes
1 answer

Hibernate generates cross join instead of left join

I'm using hibernate 3.6.10.Final and MySQL 5.5. I wrote a dynamic query like this: "from " + clazz.getName()+ " ORDER BY "+sortField+" "+sortDirection My hibernate entities have a many to one parent/child relationship. if the query is ordered by a…
Tostis
  • 386
  • 2
  • 5
  • 13
3
votes
1 answer

Entity framework: cross join causes OutOfMemoryException

I've got a table with 1.5 million records on a SQL Server 2008. There's a varchar column 'ReferenzNummer' that is indexed. The following query executed in the SQL Management Studio works and is fast: SELECT v1.Id, v2.Id FROM Vorpapier as v1 cross…
Stefan
  • 121
  • 7
3
votes
1 answer

Cross join N sets of rows in same table

I have a generic "Dimension" and "DimensionMember" tables. CREATE TABLE [dbo].[Dimension] ( [ID] [int] NOT NULL IDENTITY(1, 1), [Label] [nvarchar] (255) ) CREATE TABLE [dbo].[DimensionMember] ( [ID] [int] NOT NULL IDENTITY(1, 1), [Label]…
Maxim Eliseev
  • 3,248
  • 4
  • 29
  • 35
2
votes
1 answer

Crossjoin using QueryOver

How could I replace the HQL query below using QueryOver API? var sql = "from Role r, Action a where r.Active = :active and a.Active = :active"; var result = manager.Session.GetISession().CreateQuery(sql) .SetBoolean("active",…
2
votes
2 answers

Join table on itself without two queries

I've seen people recommending cross joining a table on itself by doing this: SELECT * FROM tbl AS A, tbl AS B WHERE A.col1 = 1 AND B.col1 = 1 But here, the engine needs to iterate through all of the rows in tbl twice to match the two queries to the…
carlbenson
  • 3,177
  • 5
  • 35
  • 54
2
votes
5 answers

Unexpected behavior for count on empty table

I have two tables table_a and table_b in a PostgreSQL 13 database, having UUIDs primary key columns. table_a has multiple entries, whereas table_b is empty (no entry). The following query returns the expected result, namely entry_count_a larger than…
DaHoC
  • 314
  • 1
  • 4
  • 14
2
votes
4 answers

Calculate diff time for combinations of two columns

I need to calculate the time it takes for trucks to go from one station to another. I want to select any origin and destination in my dataset and calculate the average time it took for all trucks. This is how the data could look like this: Truck…