×

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 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 MONTHNAME() function

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

3 minutes read.

MySQL RTRIM() function

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

2 minutes read.

MySQL Full text index

Full text index The full-text index in the table isused to search the full text of the data. This index assigns to the table using a "FULLTEXT" keyword. First, the table...

3 minutes read.

MySQL LOAD_FILE() function

In this context, we will learn how we can use the MySQL LOAD_FILE() function with proper syntax and good examples. Introduction of MySQL LOAD_FILE() function The LOAD_FILE() function in MySQL reads a...

3 minutes read.

MySQL COUNT function

The count function returns the number of rows in the table. This function shows either the entire rows count or the required row count of the table. It determines the...

5 minutes read.

MySQL Triggers

MySQL trigger is a function of the stored procedure to respond to the system program. This function responds and runs any data table event automatically. You can use it for...

5 minutes read.

MySQL SOUNDEX() function

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

3 minutes read.

find_in_set() function in MySQL

This Function is used for finding the position of a particular string from the list of strings. For suppose if the specified string is repeated multiple times, then this functions...

4 minutes read.

MySQL IF statement

The IF statement shows the true condition of the control flow function. The first condition is necessary to fulfill the requirement. The other condition is optional in the "IF" statement....

3 minutes read.

MySQL REVERSE() function

In this context, we will learn how we can use the MySQL REVERSE() function with proper syntax and good examples. Introduction of MySQL REVERSE() function This function could be used to reverse...

2 minutes read.

MySQL ASIN() Function

In this context we will learn how we can use the ASIN() function in MySQL with proper syntax and good example. Introduction of MySQL ASIN() function The arc sine value of a...

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 INNER JOIN

MySQL inner join connects two tables by using their common columns. It is the basic join of the MySQL system. It is used to returns only those results from the...

4 minutes read.

MySQL LOG() function

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

3 minutes read.

MySQL Character Length Function

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

2 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 DATE_ADD() function

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

3 minutes read.

MySQL LN() function

In this context, we will learn how we can use the MySQL LN() function with proper syntax and good examples. Introduction of MySQL LN() function For evaluation of the natural logarithm of...

2 minutes read.

MySQL Features

Features of MySQL MySQL is a relational database management system (RDBMS): It is a collection of many programs and makes relations with many other programs. Easy to use MySQL database: MySQL...

2 minutes read.