Home Python C Language C ++ HTML 5 CSS Javascript Java Kotlin SQL DJango Bootstrap React.js R C# PHP ASP.Net Numpy Dart Pandas Digital Marketing

CROSS JOIN - Cartesian Product of Two Tables in SQL


The CROSS JOIN in SQL is a type of join that returns the Cartesian product of two tables. In other words, it combines every row from the first table with every row from the second table, resulting in a large number of rows in the result set. This type of join does not require any condition to match the rows between the two tables.

1. Syntax of CROSS JOIN

The basic syntax for a CROSS JOIN is as follows:

      SELECT column1, column2, ...
      FROM table1
      CROSS JOIN table2;
    

In this syntax:

2. Example of CROSS JOIN

Consider two tables: Products and Colors. The Products table contains a list of products, and the Colors table contains a list of available colors. If you want to see every possible combination of products and colors, you can use a CROSS JOIN:

      SELECT Products.ProductName, Colors.Color
      FROM Products
      CROSS JOIN Colors;
    

This query will return all possible combinations of product names and colors, creating a Cartesian product of the two tables. For example, if there are 3 products and 4 colors, the result will contain 12 rows.

3. Example of CROSS JOIN with 3 Products and 4 Colors

If the Products table contains:

      ProductID | ProductName
      ----------------------
      1         | Shirt
      2         | Pants
      3         | Jacket
    

And the Colors table contains:

      ColorID | Color
      ----------------
      1       | Red
      2       | Blue
      3       | Green
      4       | Black
    

The result of the CROSS JOIN would be:

      ProductName | Color
      ----------------------
      Shirt       | Red
      Shirt       | Blue
      Shirt       | Green
      Shirt       | Black
      Pants       | Red
      Pants       | Blue
      Pants       | Green
      Pants       | Black
      Jacket      | Red
      Jacket      | Blue
      Jacket      | Green
      Jacket      | Black
    

As shown, the query creates all combinations of ProductName and Color, resulting in a Cartesian product.

4. Key Points to Remember About CROSS JOIN

5. Example of CROSS JOIN with Multiple Tables

You can use CROSS JOIN with more than two tables. For example, if you have another table called Sizes containing size information, you can perform a CROSS JOIN between the three tables:

      SELECT Products.ProductName, Colors.Color, Sizes.Size
      FROM Products
      CROSS JOIN Colors
      CROSS JOIN Sizes;
    

This query will return every possible combination of ProductName, Color, and Size from the three tables.

6. Performance Considerations with CROSS JOIN

Since a CROSS JOIN creates the Cartesian product of the two tables, the size of the result set can grow quickly. For example:

It is important to use CROSS JOIN thoughtfully, especially in situations where large tables are involved or when only specific combinations are needed.

7. Conclusion

The CROSS JOIN in SQL is a useful tool when you need to combine every row from one table with every row from another table, producing a Cartesian product. It does not require any join condition, which makes it different from other types of joins such as INNER JOIN, LEFT JOIN, or RIGHT JOIN. However, because it can generate a large result set, it's important to use it carefully, especially with large tables.



Advertisement

Advertisement

Advertisement





Q3 Schools : India


Online Complier

HTML 5

Python

java

C++

C

JavaScript

Website Development

HTML

CSS

JavaScript

Python

SQL

Campus Learning

C

C#

java