Back to articles list Articles Cookbook
Updated: 28th Oct 2024 4 minutes read

What Is CROSS JOIN in SQL?

What is a CROSS JOIN in SQL, and when should you use it? We answer those questions – and give you some examples of CROSS JOIN you can practice for yourself – in this article.

CROSS JOINs, a type of SQL JOIN, create all possible row combinations from two tables.

If you're new to JOINs and want to learn more, check out our comprehensive SQL JOINs course, which includes over 90 hands-on exercises on various JOIN types. This interactive course will help you master JOINs, a critical skill for data analysis.

The Syntax of CROSS JOIN

To understand the essence of a SQL CROSS JOIN, let's take a look at the basic syntax:

SELECT *
FROM table1
CROSS JOIN table2;

Breakdown:

  • SELECT *: Retrieves all (*) columns from the resulting combined table.
  • FROM table1: Specifies the first table used in the CROSS JOIN operation.
  • CROSS JOIN: Indicates that we want to perform a cross-join operation.
  • table2: Specifies the second table involved in the CROSS JOIN operation.

The syntax is similar to that of any other SQL JOIN. The exception is that a CROSS JOIN requires no join condition – it combines every row from one table with every row from another table, regardless of any specific condition or relationship between the rows. You can find more information about this in Can You Join Two Tables Without a Common Column?

How CROSS JOIN Works

Now let's look at a complete example. Imagine that your wardrobe is organized into the two tables below:

Tops
top_itemcolorsize
sweaterredM
shirtgreenM
tank_topyellowS
Bottoms
bottom_itemcolorsize
jeansblueM
skirtgreenS
shortsyellowS

SQL Query:

SELECT *
FROM Tops
CROSS JOIN Bottoms;

Output:

Here's an illustration of what CROSS JOIN does. It combines all rows from one table with all rows from the other table.

CROSS JOIN

Here's a table you'd get in the result:

top_itemcolorsizebottom_itemcolorsize
sweaterredMjeansblueM
sweaterredMskirtgreenS
sweaterredMshortsyellowS
shirtgreenMjeansblueM
shirtgreenMskirtgreenS
shirtgreenMshortsyellowS
tank_topyellowSjeansblueM
tank_topyellowSskirtgreenS
tank_topyellowSshortsyellowS

In this simple example, the CROSS JOIN connects each type of top to each type of bottom, allowing you to see every outfit combination in your wardrobe.

Notice that since the Tops table has 3 rows and the Bottoms table has 3 rows, the resulting table has 9 (3 times 3) rows. This multiplication principle applies across all CROSS JOIN operations. For more examples, check out our Illustrated Guide to the SQL CROSS JOIN.

Performance Considerations

When using CROSS JOIN in SQL, it is critical to consider the performance implications, particularly given the potentially massive result set generated by an unrestricted CROSS JOIN. This operation produces a Cartesian product that grows exponentially in proportion to the size of the input tables. Let's look at some tips for avoiding performance issues.

  • Limit the size of input tables: Before executing a CROSS JOIN, refine the input tables by applying appropriate filters. Use WHERE clauses or subqueries to narrow down the dataset, thus minimizing the number of rows to be combined.
  • Apply additional filtering: After the CROSS JOIN, employ supplementary filtering conditions to further refine the result set. This aids in reducing the final output's size and focusing solely on pertinent data, enhancing query performance.
  • Consider alternative join types: Evaluate whether an unrestricted CROSS JOIN is truly indispensable. Depending on specific requirements, explore alternative join types like INNER JOIN, LEFT JOIN, or RIGHT JOIN, which may be more efficient and more tailored to your needs.
  • Optimize query execution: Ensure that your database is optimized for efficient query execution. Implementing indexes, particularly on join columns, will expedite performance – especially when handling extensive datasets.

By incorporating these performance considerations for the use of CROSS JOIN, you can mitigate undue strain on your database and ensure the smooth execution of queries, even when working with extensive datasets.

Want to Learn More About CROSS JOINs?

In conclusion, CROSS JOIN in SQL is a useful tool for creating comprehensive datasets by combining every row from one table with every row from another. By understanding the fundamentals of CROSS JOIN and considering its performance implications, analysts can unleash its full potential for exploring complex relationships within datasets and extracting valuable insights.

To fully leverage the capabilities of CROSS JOIN and other SQL JOIN types, LearnSQL.com has created the SQL JOINs course. This interactive course includes hands-on exercises covering various JOIN types and is designed to help you understand the complexities of JOIN operations.

You can also read other articles on our blog, such as The Top 10 SQL JOIN Interview Questions and How to Answer Them. And don't forget about our handy SQL JOIN Cheat Sheet, which is always available to assist you in your SQL exploration. So, what are you waiting for? Elevate your skills today!