The CROSS JOIN in SQL is used to join two or more tables by combining each row of the first table with each row of the second table and so on. It returns the Cartesian product of the two tables, meaning it will return all possible combinations of rows from both tables.
Here’s an example:
Consider two tables, “students” and “courses”. The “students” table has the following data:
student_id | name
---------- | ------
1 | John
2 | Jane
3 | Jack
And the “courses” table has the following data:
course_id | course_name
--------- | -----------
A | Mathematics
B | Science
C | History
A CROSS JOIN between these two tables would produce the following result:
student_id | name | course_id | course_name
---------- | ------| --------- | -----------
1 | John | A | Mathematics
1 | John | B | Science
1 | John | C | History
2 | Jane | A | Mathematics
2 | Jane | B | Science
2 | Jane | C | History
3 | Jack | A | Mathematics
3 | Jack | B | Science
3 | Jack | C | History
As you can see, the CROSS JOIN has combined each row of the “students” table with each row of the “courses” table. This is useful when you want to find all possible combinations between two tables. However, it can result in a large number of rows, so be careful when using it.
How to use cross join?
SELECT *
FROM table1
CROSS JOIN table2;
In this example, table1
and table2
are the names of the tables you want to join. The CROSS JOIN
keyword combines every row from table1
with every row from table2
, resulting in a cartesian product of the two tables. The *
in the SELECT
statement selects all columns from both tables.
Also check WHAT IS GIT ? It’s Easy If You Do It Smart
You can also visite the Git website (https://git-scm.com/)