SQL CROSS JOIN

Challenge Inside! : Find out where you stand! Try quiz, solve problems & win rewards!

Learn via video course

DBMS Course - Master the Fundamentals and Advanced Concepts
DBMS Course - Master the Fundamentals and Advanced Concepts
By Srikanth Varma
Free
star5
Enrolled: 1000
DBMS Course - Master the Fundamentals and Advanced Concepts
DBMS Course - Master the Fundamentals and Advanced Concepts
Srikanth Varma
Free
5
icon_usercirclecheck-01Enrolled: 1000
Start Learning

Overview

The CROSS JOIN is used to generate a paired combination of each row of the first table with each row of the second table. This join type is also known as cartesian join.

Scope

In this article, we will discuss

  • What is CROSS JOIN with example.
  • Syntax of SQL CROSS JOIN.

What is CROSS JOIN in SQL?

CROSS JOIN is useful when you need all combinations. It maps each row of the first table with all the rows from the second table.

CROSS JOIN is also called Cartesian Product – if the first table has X rows and the second table has Y rows, it will return X*Y rows.

For example, if you have 3 distinct shirts and 2 distinct jeans and you want all possible combinations they can be used in, CROSS JOIN can help.

example of cross join Mapping each row from SHIRTS to JEANS gives us 6 (3*2) combinations.

  • Blue Shirt & Black Jeans
  • Blue Shirt & Blue Jeans
  • Black Shirt & Black Jeans
  • Black Shirt & Blue Jeans
  • White Shirt & Black Jeans
  • White Shirt & Blue Jeans

Syntax of CROSS JOIN in SQL

There are two ways to implement CROSS JOIN in SQL.

  • CROSS JOIN clause

Example of CROSS JOIN in SQL

We are hosting a survey on our website and the marketing team decided to broadcast the word a week in advance while we are still drafting questions for the survey.

Users can register for the survey throughout the week. When the survey starts, we want to get the cartesian product of all registered users and the finalized questions and save them in a table where we can record the answers of each user against all the questions.

We will be working with three tables, Users Table to store the details of registered users. Questions Table to store the drafted questions for our survey and an Answers Table which will store the result of CROSS JOIN in SQL.

UsersQuestions
idnameemailidquestion
1Tomtom@gmail.com1Do you shop online?
2Dickdick@gmail.com2Do you rate products bought online?
3Harryharry@gmail.com3Do you prefer COD?
4Do you own a Credit Card?

We need all combinations, so using CROSS JOIN in SQL here – we get 12 rows

Cartesian Product of ‘users’ and ‘questions’ –

idnameemailidquestion
1Tomtom@gmail.com1Do you shop online?
2Dickdick@gmail.com1Do you shop online?
3Harry1Do you shop online?
1Tomtom@gmail.com2Do you rate products bought online?
2Dickdick@gmail.com2Do you rate products bought online?
3Harryharry@gmail.com2Do you rate products bought online?
1Tomtom@gmail.com3Do you prefer COD?
2Dickdick@gmail.com3Do you prefer COD?
3Harryharry@gmail.com4Do you prefer COD?
1Tomtom@gmail.com4Do you own a Credit Card?
2Dickdick@gmail.com4Do you own a Credit Card?
3HarryDo you own a Credit Card?

Using SQL INSERT INTO SELECT Statement to get all combinations in the ‘answers’ table.

We see that each user is mapped against all four questions we created for our survey, now we can record the answer by a simple UPDATE API.

user_idquestion_idanswer
11Yes
12Yes
13Yes
14Yes
21Yes
22Yes
23No
24No
31Yes
32No
33Yes
34No

Conclusion

As we see, CROSS JOIN in SQL is extremely powerful in cases where we want to work with all combinations or cartesian products of tables.

If we have three tables with all possible values for hours (0 to 23), minutes (0 to 59), and seconds (0 to 59), and if we CROSS JOIN among them, we will get 86400 rows. One row for each second in a day. Such implementations make it convenient to generate test data.

Most of the time you will want to use powerful restrictive features of SQL and avoid pulling unnecessary rows while retrieving data from tables.

Hopefully, this article will redirect you in the right direction if you find yourself attempting any work with Cartesian products of tables.