×

MySQL CROSS JOIN

MySQL CROSS JOIN combines all possibilities of the two or more tables and returns the result that contains every row from all contributing tables. It links several columns from the multiple tables in any order. This join can link any column of the first table with another table. The "cross join" links two tables with each row. It is also known as CARTESIAN JOIN, which provides the Cartesian product of all associated tables.

Syntax

The "cross join" syntax shows below.

SELECT * FROM table1 CROSS JOIN table2; 

The "cross join" with id syntax shows below.

SELECT * FROM table1.id CROSS JOIN table2.id; 

Examples of the MySQL cross join

1) Example:

Execute the below query to join two tables. Two tables join required columns using specified conditions. Here, the query using the "ON" condition with cross join.

mysql> select d.department_name, d.department_id, 
s.subject_name, s.subject_id
FROM department d
CROSS JOIN subject s
ON d.department_id = s.subject_id;

OUTPUT

CROSS JOIN

The above image shows combined columns of the two tables using cross join. The "department_name" and "department_id" columns display in the department table. The "subject_name" and "students" columns display in the subject table.

2) Example:

Execute the below query to join two tables. Two tables join required columns using specified conditions. Here, the query selects the entire columns of the table.

mysql> select *
FROM department d
CROSS JOIN subject s
ON d.department_id = s.subject_id;

OUTPUT

CROSS JOIN

The above image shows the entire columns of the department and student tables because of the cross join.

3) Example:

Execute the below query to join two tables. Two tables join all columns using the "CROSS JOIN" query. This query uses the ON and WHERE clause with CROSS JOIN.

mysql> select d.department_name, d.department_id, s.subject_name, s.subject_id
FROM department d
CROSS JOIN subject s
ON d.department_id = s.subject_id 
WHERE subject_id < 3;

OUTPUT

CROSS JOIN

The above image returns the required columns and rows of the two tables using cross join. It uses clauses and conditions for two tables in a single query. Here, we can see four columns and two rows.

4) Example:

Execute the below query to join two tables with the required columns. This query uses the "ON" and the "ORDER BY" clause with CROSS JOIN.

mysql> select d.department_name, d.department_id, s.subject_name, s.subject_id
FROM department d
CROSS JOIN subject s
ON d.department_id = s.subject_id 
ORDER BY 
department_id DESC,
subject_id DESC;

OUTPUT

CROSS JOIN

The above output image shows the given columns of the two tables using cross join. Here, you can show four columns in descending order of data.

5) Example:

Execute the below cross join query to join two tables. Two tables join required columns using specified conditions. For example, this query uses "WHERE" with "AND" conditions.

mysql> select d.department_name, d.department_id,
s.subject_name, s.subject_id
FROM department d
CROSS JOIN subject s
WHERE d.department_name = "Bachelor of Technology"
AND  s.subject_name = "Electronics";

OUTPUT

CROSS JOIN

The above image shows the required columns of the two tables. The cross query works with the logical condition.

6) Example:

Execute the below cross join query to join two tables. Two tables join required columns using specified conditions. For example, this query uses "WHERE" with "OR" conditions.

mysql> select d.department_name, d.department_id,
s.subject_name, s.subject_id
FROM department d
CROSS JOIN subject s
WHERE d.department_name = "Bachelor of Technology"
OR  s.subject_name = "Electronics";

OUTPUT

CROSS JOIN

The above image shows the columns of the two tables. This query shows the required columns of the department table and student table. The cross query works with the logical condition. Both conditions fulfill using the "OR" logical condition.

7) Example: the cross join with the "USING" clause example shows below.

Execute the below query to join two tables. For example, the "CROSS JOIN" query uses "USING" clause with the "subject_id" foreign key.

mysql> select d.department_name, d.department_id,
    -> s.subject_name, s.subject_id
    -> FROM department d
    -> CROSS JOIN subject s
    -> USING (subject_id);

OUTPUT

CROSS JOIN

The above output table displays combined columns of the department and subject tables. The "subject_id" column connects two tables using a foreign key. The above table works on the cross join query with the "USING" clause.

8) Example: the cross joins with the "GROUP BY" clause example shows below.

Execute the below query to join two tables. Two tables join required columns using conditions. For example, the "CROSS JOIN" query uses "GROUP BY" clause with the "subject_id" foreign key. It also uses AND logical operator.

mysql> select d.department_name, d.department_id,
    -> s.subject_name, s.subject_id
    -> FROM department d
    -> CROSS JOIN subject s
    -> USING (subject_id)
    -> WHERE d.department_name = " Bachelor of Engineering"
    -> GROUP BY subject_id;

OUTPUT

CROSS JOIN

The above output image displays combined columns of the department and subject tables. The "subject_id" column connects two tables using a foreign key.

9) Example: the cross join with the multiple clause example shows below.

Execute the below query to join two tables. This example uses "ORDER BY" and "using" clauses with the "subject_id" foreign key. It also uses OR logical operator.

mysql> select d.department_name, d.department_id,
    -> s.subject_name, s.subject_id
    -> FROM department d
    -> CROSS JOIN subject s
    -> USING (subject_id)
    -> WHERE d.department_name = "Bachelor of Technology"
    -> OR  s.subject_name = "Computer Science"
    -> ORDER BY department_id DESC, subject_id DESC;

OUTPUT

CROSS JOIN

The above output table displays combined columns of the department and subject tables. The "subject_id" column connects two tables using a foreign key. The above table works on the cross join query with the "USING" clause. Here, the table applies a cross join query with the "ORDER BY" clause. The table data display in descending order.


Related Topics

MySQL POWER() function

In this context, we will learn how we can use the MySQL POWER() function with proper syntax and good examples. Introduction of MySQL POWER() function The value of a number raised to...

3 minutes read.

MySQL COALESCE function

This statement displays the first non-null value of the table data. The COALESCE function returns a null value when all values of the table are null or do not find...

3 minutes read.

MySQL Composite Index

Composite Index A composite index is an index that is used on multiple columns. MySQL management system manages multiple columns simultaneously. Therefore, the single index contains multiple columns in one query...

3 minutes read.

MySQL Stored Procedure

MySQL creates the "stored procedure" function to operate database information. You can use parameters, blocks, and statements to create a new procedure. The procedure requires a database table to use...

3 minutes read.

MySQL NATURAL JOIN

The natural join combines multiple tables using a column with the same name and data type. This operation combines a row of the two tables using common columns. This join...

4 minutes read.

MySQL RIGHT() Function

In this context, we will learn how we can use the MySQL RIGHT() Function with proper syntax and good examples. Introduction of MySQL RIGHT() function The RIGHT() function in MySQL is used...

3 minutes read.

MySQL SEC_TO_TIME() function

In this context, we will learn how we can use the MySQL SEC_TO_TIME() function with proper syntax and good examples. Introduction of MySQL SEC_TO_TIME() function This function in MySQL is used to...

2 minutes read.

MySQL CEIL() function

In this context, we will learn how we can use the MySQL CEIL () function with proper syntax and good examples. Introduction of MySQL CEIL() function Input is taken by the CEIL()...

3 minutes read.

MySQL FORMAT() function

In this context, we will learn how we can use the MySQL FORMAT() function with proper syntax and good examples. Introduction of MySQL FORMAT() function This function in MySQL helps to format...

2 minutes read.

MySQL PERIOD_DIFF() function

In this context, we will learn how we can use the MySQL PERIOD_DIFF() function with proper syntax and good examples. Introduction of MySQL PERIOD_DIFF() function This function in MySQL is used to...

2 minutes read.

MySQL FROM_BASE64() function

In this context, we will learn how we can use the MySQL FROM_BASE64()() function with proper syntax and good examples. Introduction of MySQL FROM_BASE64()() function Basically, the FROM_BASE64() function In MySQL decodes...

2 minutes read.

MySQL EQUI JOIN

An equijoin is an operation that combines multiple tables based on equality or matching column values in the associated tables. This operation links more than two tables based on a...

5 minutes read.

MySQL GREATEST function

This statement displays the greatest values of the table. The GREATEST function returns a null value when the table contains a null value. The GREATEST function needs a minimum of...

2 minutes read.

MySQL Control flow function

The control flow function uses values or operands for logical operations. These functions can works on single and multiple conditions. The control flow function is based on the Boolean expression....

3 minutes read.

MySQL AVG function

This function works on numerical data type values. The average function shows the average value of the data set. If an average function returns a null value, then the row...

5 minutes read.

MySQL UPDATE JOIN

The UPDATE JOIN is a MySQL statement used to perform cross-table updates that means we can update one table using another table with the JOIN clause condition. The "update join"...

5 minutes read.

MySQL Environmental Setup

The MySQL Environmental Setup MySQL is free, open-source, and cross-platform software, which can be downloaded from its official website. MySQL management system installs on Linux, macOS, and windows. MySQL requires a framework...

6 minutes read.

MySQL FIELD() function

In this context, we will learn how we can use the MySQL FIELD() function with proper syntax and good examples. Introduction of MySQL FIELD() function The index position of a mentioned value...

3 minutes read.

MySQL Vs MongoDB

What is MongoDB? MongoDB is a distributed, open-source, cross-platform document-based database which was created to scale and develop applications simply. It was created as a NoSQL database by MongoDB Inc. MongoDB gets...

6 minutes read.

MySQL RADIANS() function

In this context, we will learn how we can use the MySQL RADIANS() function with proper syntax and good examples. Introduction of MySQL RADIANS() function RADIANS() function in MySQL is used to...

2 minutes read.