It combines each row from the first table with every row from the second table.
Here's the syntax for a CROSS JOIN:
SELECT column1, column2
FROM table1
CROSS JOIN table2;
Suppose we have the following tables:
students table:
+----+-----------+-----+
| id | name | age |
+----+-----------+-----+
| 1 | Alice | 20 |
| 2 | Bob | 22 |
| 3 | Charlie | 21 |
+----+-----------+-----+
courses table:
+----+------------+
| id | name |
+----+------------+
| 1 | Math |
| 2 | Physics |
| 3 | Chemistry |
+----+------------+
To perform a CROSS JOIN to generate all possible combinations of students and courses, you would use the following query:
SELECT students.name AS student_name, courses.name AS course_name
FROM students
CROSS JOIN courses;
This query would result in a table with every combination of students and courses:
+--------------+-------------+
| student_name | course_name |
+--------------+-------------+
| Alice | Math |
| Alice | Physics |
| Alice | Chemistry |
| Bob | Math |
| Bob | Physics |
| Bob | Chemistry |
| Charlie | Math |
| Charlie | Physics |
| Charlie | Chemistry |
+--------------+-------------+