Trusted answers to developer questions

What is the SQL CROSS JOIN?

Get Started With Data Science

Learn the fundamentals of Data Science with this free course. Future-proof your career by adding Data Science skills to your toolkit — or prepare to land a job in AI, Machine Learning, or Data Analysis.

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:

/*CROSS JOIN*/
SELECT *
FROM employee
CROSS JOIN department

RELATED TAGS

sql
database
join
cross join
Copyright ©2024 Educative, Inc. All rights reserved
Did you find this helpful?