What is the SQL CROSS JOIN?

In SQL, the CROSS JOIN is used to combine each row of the first table with each row of the second table. It is also known as the Cartesian join since it returns the Cartesian product of the sets of rows from the joined tables.

svg viewer

Syntax

There are two implementations of the CROSS JOIN statement:

  • Using the CROSS JOIN syntax.
  • Using the FROM clause without using a WHERE clause.

1. Using the CROSS JOIN clause

In this implementation, we specify the keyword CROSS JOIN in between the table names we want to join.

SELECT      [column names]
FROM        [Table1]
CROSS JOIN  [Table2]

2. Using the FROM clause without using a WHERE clause.

In this implementation we use the FROM keyword along with the table names; these names are separated by commas.

SELECT [column names]
FROM   [Table1], [Table2]

Examples

Consider the two tables, called employee and department, below:

svg viewer

Executing cross join on these gives us the following output:


Copyright ©2024 Educative, Inc. All rights reserved