MySQL Tutorial

MySQL Tutorial MySQL Features MySQL Database Introduction MySQL Environmental Setup MySQL Data Types MySQL variable MySQL Advance table Query MySQL database queries MySQL Entity-Relationship Model MySQL Table Query MySQL Operators MySQL logical conditions MySQL Queries MySQL Clauses Clustered vs Non-Clustered Index MySQL Full text index MySQL Descending Index MySQL Invisible Index MySQL Composite Index MySQL Prefix index MySQL Index MySQL Create index MySQL Drop Index MySQL Show index MySQL Unique index MySQL Table MySQL Variable MySQL View MySQL Constraints MySQL Command Line Client Basic Queries MySQL Stored Procedure MySQL IF Statement MySQL Subquery MySQL Triggers

MySQL Join

MySQL Join MySQL CROSS JOIN MySQL DELETE JOIN MySQL EQUI JOIN MySQL INNER JOIN MySQL Union MySQL NATURAL JOIN MySQL RIGHT JOIN MySQL SELF JOIN MySQL UPDATE JOIN

MySQL Function

MySQL Function MySQL AVG() Function MySQL SUM() Function MySQL String() Function MySQL Advance() Function MySQL Aggregate() Function MySQL COALESCE() Function MySQL Control Flow Function MySQL COUNT() Function MySQL Date And Time Function MySQL GREATEST() Function MySQL ISNULL() Function MySQL LEAST() Function MySQL Math() Function MySQL MAX() Function MySQL MIN() Function MySQL find_in_set() function MySQL ASIN() Function MySQL CEIL() function MySQL CEILING() function MySQL TAN() Function MySQL Truncate() Function MySQL FLOOR() function MySQL LN() function MySQL LOG2() function MySQL LOG10() function MySQL MOD() function MySQL PI() function MySQL POW() function MySQL RADIANS() function MySQL RAND() function MySQL ROUND() function MySQL Character Length Function MySQL Current Date Function MySQL Date Add Function MySQL Date Format Function MySQL Datediff Function MySQL Day Function MySQL Elt Function MySQL Export Set Function MySQL Field Function MySQL Format Function MySQL From Base64 Function MySQL Hex Function MySQL Insert Function MySQL Instr Function MySQL Length Function MySQL CONCAT() function MySQL FIND_IN_SET() function MySQL LIKE() function MySQL LOAD_FILE() function MySQL LOCATE() function MySQL LOG() function MySQL MONTHNAME() function MySQL NOW() function MySQL PERIOD_ADD() function MySQL PERIOD_DIFF() function MySQL POWER() function MySQL QUARTER() function MySQL REVERSE() function MySQL RIGHT() Function MySQL RPAD() function MySQL RTRIM() function MySQL SEC_TO_TIME() function MySQL SOUNDEX() function

Questions

Which Clause is Similar to Having Clause in MySQL

Misc

MySQL Error 1046 - No Database Selected Failed to Start MySQL Service Unit MySQL Service Unit not Found Import MySQL Connector Mudule not Found Error No Module Named MySQL Joins Available in MySQL MySQL Docs MySQL Download For Windows 7 64 Bit MySQL Error Code 1064 MySQL Export MySQL History MySQL Host MySQL Import MySQL Drop All Tables MySQL Drop MySQL Error Code 1175 MySQL Events MySQL Except MYSQL Foreign Key Constraint MySQL If Exists MySQL IndexOf MySQL List All Tables json_extract in MySQL TIMESTAMPDIFF in MySQL MySQL Syntax Checker Sudo MySQL Secure Installation

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.