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
-1
votes
1 answer

Cross or Inner Join two tables that have one non-unique column in common

I have two tables; the first shows all (unique) persons. It contains a column showing the type of house each person would like to have. The second table shows all unique ads. Each row also contains the type of house in the ad. So HouseType is not…
jeeest
  • 1
  • 2
-1
votes
1 answer

Setting set of text values for each ID

I am working in SQL server and have two tables in my database, I am attaching their sample screenshots below: Table 1:table_1 And the second table is as shown in the image below: Table 2:table_2 I am trying to assign all the Korean language values…
-1
votes
2 answers

MySQL combine results of two unrelated tables

I'm trying to merge two query results in to one: Query 1 and the reulsts: SELECT MONTHNAME(tblFeesPaid.Pay_Date) AS 'Month', SUM(tblFeesPaid.Fees_Paid) As 'Total Fees' FROM tblFeesPaid INNER JOIN tblFeesStructure ON…
Kamweti C
  • 19
  • 6
-1
votes
1 answer

how to find consecutive timestamp and calculate sum in python

I am looking for the sum of sales made by each customer within 24 hours. For example, id timestamp sales 123 2022-10-01 12:50:55 11 124 2022-10-01 22:50:55 11 123 2022-10-01 13:50:55 11 123 2022-10-02 12:50:55 11 123 …
alli666
  • 11
  • 3
-1
votes
2 answers

How to use a table twice without run it 2 times?

Right now I got a structure that outputs exactly the result I'm looking for, however I think it isn't as optimized as I would like to since it runs the same table twice (crafted_table). Here's the structure of my query: SELECT * FROM (crafted_table)…
Luis
  • 1
-1
votes
1 answer

Cross Join on Itself RStudio

I am interested in finding out how to convert multiple columns in RStudio into two columns by finding every possible combination excluding null values. Here is sample input: Mike Bill Pat Ty Bob Bret Mike null Dyl Ty null null Here is sample…
Zach
  • 1
-1
votes
2 answers

Does Cross Join not work between two different tables with same column name?

As written on the title, does CROSS JOIN not work for different tables with the same column name? For example, I have one table named Fruits: | name | price | | apple | 1 | | banana | 2 | and another table named Snacks: | name |…
-1
votes
2 answers

How to generate a cross join table, having one of the IDs from a range in Python?

I have a table with the total number of secondary IDs per each principal ID. So I need to generate a second table in which each row correspond to the combination of primary ID and Secondary ID. For example if for primary ID1 I have 5 as the number…
-1
votes
1 answer

JOIN multiple tables based on one reference table and count records

I have these following tables: cars: id manufacturer -------------------- 1 Nissan 2 Toyota 3 Honda 4 Volkswagen type: id type car_id -------------------------------------------- 1 maxima …
Jam1
  • 629
  • 12
  • 25
-1
votes
1 answer

Query for join three tables without coincidences

I have three tables in my ERP and I need a query, by SQL Server, for join all rows. Here are the three tables I have with its properties, filtering by product code (SL.CodProduct = 'AL4301') to simplify: TABLE…
antoine
  • 5
  • 2
-1
votes
1 answer

Oracle Cross Join based on first row

I am using Oracle database capturing transactional data. Table 1: The transaction data of users opening the survey form. A recipient with the email address could potentially open the survey form couple of times but did not fill in and submit. The…
user-DY
  • 123
  • 2
  • 4
  • 14
-1
votes
2 answers

Unpivot data while keeping one row as column

I have the following data which I am trying to unpivot. The number of columns I am dealing with goes all the way to F600 Basically the SEQ_NUM row data becomes a column named SEQ_NUM and Cells remains a column but without the SEQ_NUM row and a new…
-1
votes
1 answer

How to write ON condition for CROSS JOIN in SQL SERVER

Unable to write Join condition using ON clause. This is Working: SELECT STUDENT.STD_NAME,CLASS_SUBJECT.SUB_NAME FROM STUDENT CROSS JOIN CLASS_SUBJECT WHERE STUDENT.CLS_ID=CLASS_SUBJECT.CLS_ID This is not working: SELECT…
Chandra
  • 156
  • 3
  • 11
-1
votes
1 answer

How can I create a left join using cross join in SQL?

How can I create a query equivalent to a left join without actually using left join in SQL?
Karan Bansal
  • 1,314
  • 1
  • 11
  • 16
-1
votes
1 answer

Cross joining two tables with "using" instead of "on"

I found a SQL query in a book which i am not able to understand. From what i understand there are two tables - date which has a date_id and test_Date column, the 2nd table has date_id and obs_cnt. select t1.test_date ,sum(t2.obs_cnt) from…
Raj
  • 1,049
  • 3
  • 16
  • 30