Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

sql
database
join
cross join

What is the SQL CROSS JOIN?

Educative Answers Team

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

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 ©2022 Educative, Inc. All rights reserved

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring