Use Sophia to knock out your gen-ed requirements quickly and affordably. Learn more
×

Cross Joins

Author: Sophia

what's covered
This tutorial explores using cross join to join data in tables in two parts:
  1. Introduction
  2. Examples With Simple Tables

1. Introduction

A cross join is a unique type of join that creates a combination of every row from two different tables. It is also called a cross product or cartesian join, since all row combinations are included in the result. One example where we could use a cross join is if we wanted to get all of the potential combinations of a product, like all of its colors and sizes.

It is important to note that these cases are rare, and most scenarios where we have a cartesian join between two tables means that we have a missing join statement between a common attribute.

The structure of the cross join looks like this:


SELECT <columnlist>
FROM <table1>
CROSS JOIN <table2>;

Notice that there are no details of how the tables are joined. This statement would result in the same result set as if we did:


SELECT <columnlist>
FROM <table1>,<table2>;


Or, we could even use an inner join with a condition that always evaluates to true to force a cross join:


SELECT <columnlist>
FROM <table1>
INNER JOIN <table2> ON true;

2. Examples With Simple Tables

Let's give this a try by creating a couple of tables for size and color and joining them together.


CREATE TABLE color ( color_id INT PRIMARY KEY, color_name VARCHAR (50) NOT NULL );

CREATE TABLE size ( size_id INT PRIMARY KEY, size_name VARCHAR (30) NOT NULL );

INSERT INTO color (color_id, color_name)
VALUES (1, 'Blue'), (2, 'Red'), (3, 'Yellow');

INSERT INTO size (size_id, size_name)
VALUES (1, 'Small'), (2, 'Medium'), (3, 'Large');

Now that the tables have been created, we should expect to see 9 rows returned from the cross join, as there are 3 rows in the color table and 3 in the size table:


SELECT color_id, color_name, size_id, size_name
FROM color
CROSS JOIN size;

CROSS JOIN Example

Running each of the other queries returns the same results:


SELECT color_id, color_name, size_id, size_name
FROM color, size;

SELECT color_id, color_name, size_id, size_name
FROM color
INNER JOIN size ON true;

CROSS JOIN Example 2

As you can see, the cross join has taken each color and matched it to each size to display all possible combinations. This type of combination can become problematic as the size of the data gets larger. In our example, we only have 3 rows in the color table and 3 rows in the size table. But imagine if we had 1,000 rows in one table and 1,000 rows in another table. The result would have 1,000,000 rows in the result.


Video Transcript

try it
Your turn! Open the SQL tool by clicking on the LAUNCH DATABASE button below. Then enter in one of the examples above and see how it works. Next, try your own choices for which columns you want the query to provide.

summary
The CROSS JOIN clause creates a combination of every row from two or more different tables.

Source: Authored by Vincent Tran