MySQL Tutorial
MySQL Installation and Configuration
MySQL Database Operations
Database Design
MySQL Data Types
MySQL Storage Engines
MySQL Basic Operations of Tables
MySQL Constraints
MySQL Operators
MySQL Function
MySQL Manipulate Table Data
MySQL View
MySQL Indexes
MySQL Stored Procedure
MySQL Trigger
MySQL Transactions
MySQL Character Set
MySQL User Management
MySQL Database Backup and Recovery
MySQL Log
MySQL Performance Optimization
A CROSS JOIN
in MySQL is used to combine all rows from two or more tables, irrespective of any matching condition. A CROSS JOIN
returns the Cartesian product of rows from both tables. This means that it will return a result set which is the number of rows in the first table multiplied by the number of rows in the second table if no WHERE clause is used along with CROSS JOIN
.
Here's a basic usage of the CROSS JOIN
:
SELECT * FROM table1 CROSS JOIN table2;
For example, consider two tables: students
and courses
. If you want to get a combination of every student with every course (perhaps to plan a schedule), you could use a CROSS JOIN
like this:
SELECT students.student_name, courses.course_name FROM students CROSS JOIN courses;
This query will return a result set with each student name combined with each course name.
Note on Usage: The CROSS JOIN
can return a large number of rows! If you have N
rows in the first table and M
rows in the second table, a CROSS JOIN
will return N*M
rows. This can quickly become a very large number, so use CROSS JOIN
with care.
Also, keep in mind that CROSS JOIN
doesn't require a join condition. If you add a WHERE
clause, it can act like an INNER JOIN
:
SELECT students.student_name, courses.course_name FROM students CROSS JOIN courses WHERE students.student_id = courses.student_id;
This query behaves like an INNER JOIN
where it only returns the rows where there's a match based on the student_id.
MySQL CROSS JOIN example:
CROSS JOIN
combines every row from the first table with every row from the second table, resulting in a Cartesian product.SELECT * FROM table1 CROSS JOIN table2;
How to use CROSS JOIN in MySQL:
CROSS JOIN
keyword to create a Cartesian product of rows from two tables.SELECT * FROM employees CROSS JOIN departments;
Cross joining tables in MySQL:
SELECT * FROM customers CROSS JOIN products;
Cartesian product with CROSS JOIN in MySQL:
CROSS JOIN
is a Cartesian product, combining every row from both tables.SELECT * FROM table1 CROSS JOIN table2;
Filtering cross join results in MySQL:
CROSS JOIN
.SELECT * FROM employees CROSS JOIN departments WHERE employees.department_id = departments.department_id;
Joining multiple tables using CROSS JOIN in MySQL:
CROSS JOIN
to combine multiple tables, creating a Cartesian product of all rows.SELECT * FROM table1 CROSS JOIN table2 CROSS JOIN table3;
MySQL CROSS JOIN vs INNER JOIN:
CROSS JOIN
produces a Cartesian product, INNER JOIN
combines rows based on a specified condition.CROSS JOIN
:SELECT * FROM customers CROSS JOIN orders;
INNER JOIN
:SELECT * FROM customers INNER JOIN orders ON customers.customer_id = orders.customer_id;
Examples of using CROSS JOIN in MySQL queries:
CROSS JOIN
operator in MySQL queries.SELECT * FROM employees CROSS JOIN departments; SELECT * FROM products CROSS JOIN categories;